templatetags - Django template tags with same name -
for example have 2 templatetags
app1 templatetags app1_tags.py def custom_tag() app2 templatetags app2_tags.py def custom_tag()
if load in template both templatetags
{% load app1_tags %} {% load app2_tags %}
i have 2 tags name custom_tag
. how can use them in template? must rename them?
i know not best solution depending on needs can helpful.
this only works if apps made or if overwrite template tags
this option give different names each tag:
app1_tags.py
@register.filter(name='custom1') def custom_tag(): # code
app2_tags.py
@register.filter(name='custom2') def custom_tag(): # code
usually if register tag without telling name, django use function filter name, if passes arg 'name' when registering filter, django use templatetag name.
if give them different names when register tag, name use load tags
{% load custom1 %} {% load custom2 %}
you need customize name of 1 of them, can use original name of other
import tag under different name
as @igor suggested, option import template want use name, let's alias avoid conflict between different tags/filters same name.
assumming want import tag project should add tag like:
your_app/template_tags/my_custom_tag
to import tag app2 on app different name need add file my_custom_tag:
from app2.template_tags.app2_tags import custom_tag register.filter(name="new_custom_tag_name", custom_tag)
after have imported tag custom_tag project new name new_custom_tag_name
Comments
Post a Comment