Using a Django template_tag on pages that extend from the view that loads the tag

情到浓时终转凉″ 提交于 2020-01-11 09:18:08

问题


I've added a template tag to my app which I load in a view located in inc/base.html. This view contains my basic HTML layout. All my other views begin {% extends "inc/base.html" %}.

In one of my views I want to refer to my template tag, which is loaded in inc/base.html using this code: {% load spb_utils %}. If I try to use on of the template tags inside base.html it works fine, but if I try it any other view, it errors unless I manually add {% load spb_utils %} to the extended view as well.

Is this behaviour intentional? Eg, if I extend a template, does Django deliberately not load any of the template tags the 'parent' template loads? Is there a smart way to globally load my tags?

thanks.


回答1:


that is the correct behaviour. extending a template does not load its template tags.

for always loading tags, see this answer, though you should carefully consider whether you really want this:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
[...]


来源:https://stackoverflow.com/questions/9647126/using-a-django-template-tag-on-pages-that-extend-from-the-view-that-loads-the-ta

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