django templates - using block.super in included template fails (exception)

非 Y 不嫁゛ 提交于 2019-12-12 07:25:58

问题


the idea is to to have multiple widgets on a page and include all js and css files needed form this 'widgets' (it's easy to manage files this way). Duplicated files is not a problem. Every widget's template is included into a page by {%include%} From inside widget's template I'm trying to add content to parent's block:

PARENT:

{%block js%}
{%endblock%}

WIDGET

{%block js%}
   {{block.super}}
   ///my widget spectyfic JS
{%end block%}

this is giving an error with {{block.super}}: Caught AttributeError while rendering: 'BlockNode' object has no attribute 'context'

I'm not sure how else can I extend block... Seems it's impossible in django... any ideas? Defining multiple blocks will not work as we don't know how many different widgets with what names will we have on each page... (and it's not a nemplate's worry)


回答1:


From the docs:

Note

The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.

If you want block.super to work then you need to use extends instead.




回答2:


I know it a bit too late but I may have some solution. Using django-sekizai you can load js and css into one place. Take a look at http://django-sekizai.readthedocs.org/en/latest/



来源:https://stackoverflow.com/questions/6566347/django-templates-using-block-super-in-included-template-fails-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!