问题
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