Django 模板继承的正确方法内容
我有一个 base.html 模板,其中包含用于字符集、google-site-verification、样式表、js...的站点范围标签。我还需要为页面特定标题标签和元描述设置块。
我想知道,我应该在我的 base.html 和该块中继承的模板混合标签中设置一个 {% block head %} ,还是应该设置特定的块,例如 {% block meta %} 和 {% block title %} 以便当 Django 渲染为 html 时标签出现在正确的位置。
这有道理吗?如果我查看源代码,所有标签混合在一个 {%block head %} 中,事情就有点乱了,但是如果我为每个标签添加特定的块,它们是有序的,但使用了更多代码......?
I have a base.html template with sitewide tags for charset, google-site-verification, stylesheets, js.... I also need to set up blocks for page specific title tags and meta descriptions.
I am wondering, should I set up a {% block head %} in my base.html and in my inherited template mix tags in that block, or should i set up specific blocks such as {% block meta %} and {% block title %} so that the tags appear in their proper places when Django renders to html.
Does this make sense? If I view source with all the tags mixed in one {%block head %} things are a bit out of order, but if I add specific blocks for each tag they are in order but use much more code...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常有三个街区。这三个已经满足了我和我的同事在过去 1.5 年的所有需求:-)
CSS 块。
JavaScript 块。
名为“head-extras”的块。通常,您希望逐页执行一些特殊操作,例如添加指向 rss feed 的链接元素。或者一些内联 JavaScript 片段。使用此块,您可以以明确的方式允许这些极端情况。
在扩展基本模板的模板中,您可以在 css 和 javascript 块中使用
{{ super }}
来获取“父级”列表并使用您自己的列表进行扩展。对于那些你只想覆盖头部中的所有内容的少数情况,我还在整个事情周围设置了一个头部块:-)
I normally have three blocks. Those three have covered all my and my colleague's needs in the last 1.5 year :-)
A block for css.
A block for javascript.
A block called "head-extras". Often you want to do something special on a page-by-page basis like adding a link element that points at your rss feed. Or some inline javascript snippet. With this block, you allow these corner cases in a clear way.
In templates that extend the base template, you can use
{{ super }}
in the css and javascript blocks to get the "parent's" list and extend it with your own.I also have a head block around the whole thing for those few cases where you just want to override everything in the head :-)