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

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

问题描述

这个想法是在一个页面上拥有多个小部件,并包含来自这个小部件"所需的所有 js 和 css 文件(以这种方式管理文件很容易).重复的文件不是问题.{%include%} 将每个小部件的模板包含在页面中从小部件的模板内部,我正在尝试将内容添加到父块:

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:

家长:

{%block js%}
{%endblock%}

小工具

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

这给出了一个错误 {{block.super}}: Caught AttributeError while rendering: 'BlockNode' object has no attribute 'context'

我不确定我还能如何扩展块...似乎在 django 中是不可能的...有什么想法吗?定义多个块是行不通的,因为我们不知道每个页面上有多少个不同名称的小部件......(这不是模板的担心)

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)

推荐答案

来自 文档:

注意

include 标签应被视为渲染此子模板并包含 HTML"的实现,而不是解析此子模板并包含其内容,就好像它是父模板的一部分一样";.这意味着包含的模板之间没有共享状态——每个包含都是一个完全独立的渲染过程.

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.

如果你想让 block.super 工作,那么你需要使用 扩展 代替.

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

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

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