guide templates django   0   393
Creating custom template tags in Django

Sometimes existing templates tags are not enough for rebellious developers. They need to create custom template tags to use.

In this article we are discussing step by step process to create and use custom template tags in Django.

Template Tags:

If all the tags are related to app then bundle them inside that app, otherwise you may create a new app and then add newly created app to INSTALLED_APPS.

Steps to create custom template tags:
  • Create a new directory inside your app and name it 'templatetags'.
  • Create  file inside this newly created directory.
  • Create tags file. Lets name is
  • Import template.
  • All the tags should be registered to template.library instance.
  • For registering tags, create a module level variable, lets name it library. So on the top of file, after import statements write register = template.library() .
  • Now lets create a very simple tag to get random number in template. For this you need to import randint.
  • write code to generate random number and register that function with library. Complete code should look like below.

from django import template
from random import randint

register = template.Library()

def random_number():
    return randint(0,999)

How to use custom tag in django templates:

Now when we have created custom tag and registered it with library, we can use it in templates.

Load the tag class in your template before using any template. {% load custom_template_tags %}

Now use custom tag any where in your html. {% random_number %} .

This will print a random number at the same place. you can store the output of tag in some variable can can use to anywhere in template. {% random_number as rnd %}. Now any where in template use {{rnd}} .

Things to take care of to make custom tags work:

  • Make sure you restarted the development server.
  • Directory name must be templatetags .
  •  must be present in directory.
  • App where templatetags  directory is created must be present in installed apps.
  • Load the tag file in the template file where tag will be used. Loading file in parent template and then expecting it would be available to all child templates, would not work. Because zen of python.
  • If tag still not working, remove all .pyc  files.

If it is still not working for you, please comment.

guide templates django   0   393

Related Articles:
How to develop a distributable Django app to block the crawling IP addresses
How to create reusable django app which can block crawling IPs from accessing your app. Creating distributable python package and upload on pypi....
Automatically updating Django website hosted on PythonAnyWhere server with every git push
How to automate the process of updating website hosted on python any where server everytime you commit and push code to git repository....
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Iterator and Generators in Python: Explained with example
understanding iterators and generators in python, creating python generators, using generators and iterators in python code...

0 thoughts on 'Creating Custom Template Tags In Django'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2018-2019