django模板 - 在包含的模板中使用block.super失败(异常) [英] django templates - using block.super in included template fails (exception)

查看:864
本文介绍了django模板 - 在包含的模板中使用block.super失败(异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想法是在一个页面上有多个小部件,并且包含这个小部件所需的所有js和css文件(这样很容易管理文件)。重复的文件不是问题。
每个小部件的模板都包含在{%include%}
的页面之中从内部小部件的模板中我正在向父级块添加内容:



父母:

  {%block js%} 
{%endblock%}

WIDGET

  {%block js%} 
{{block.super}}
///我的小部件spectyfic JS
{%end block%}


$ b $ p $ / code>



我不知道如何可以扩展块...似乎在django ...是不可能的任何想法?
定义多个块将不起作用,因为我们不知道有多少个不同的小部件,我们将在每个页面上拥有什么名称...(这不是一个模板的担心)

解决方案

文档


注意



包含标签应被视为渲染此子模板并包含HTML的实现,而不是解析此子模板并将其内容包含在内的父母。这意味着包含的模板之间没有共享状态 - 每个包含都是一个完全独立的渲染过程。


如果你想要code> block.super 工作,那么你需要使用 扩展


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)

解决方案

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.

这篇关于django模板 - 在包含的模板中使用block.super失败(异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆