Is it possible to override an existing Django Template Tag? or is it necessary to customize the template file and create a new Template Tag?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes.
As django is basically a python library (as with everything in python) you can over-write anything you’d like.
It’s not clear exactly what you’d like to do but it really is pretty easy to roll-your-own templatetag, the docs are pretty clear:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-tags
This is crazy basic but this is the kind of template I use to start building a custom template tag from:
myapp/templatetags/my_custom_tags.py
(must have
__init__.pyin this directory)Usage in your template goes like this:
You probably want to parse the
token, and you probably want some kind of__init__in the class, but it does go to show how basic it is.You can browse through the existing ‘default’ templatetags, replicate and modify them to your heart’s content.
There really is some great stuff there:
https://github.com/django/django/blob/master/django/template/defaulttags.py