Django - replacing built-in templatetag by custom tag for a whole site without {% load .. %}

泄露秘密 提交于 2019-12-20 01:35:43

问题


I want to replace the standard {% if %} template tag with a "smart if" custom tag from this snippet, because I don't want to write {% load smart_if %} every time. Also, "smart if" will come into core template system very soon.

I forgot where I saw a piece of code that does this. Does anyone know how to replace a built-in templatetag?


回答1:


Not a full solution to replace the "if" tag, but you can automatically load template tags.




回答2:


Place this somewhere you know will get run:

from django.template import add_to_builtins
add_to_builtins('mysite.myapp.templatetags.smart_if')

... while placing smart_if.py containing the smart_if code at the appropriate location. This effectively overrides the if tag with "smart if" accross the whole site.



来源:https://stackoverflow.com/questions/1639657/django-replacing-built-in-templatetag-by-custom-tag-for-a-whole-site-without

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