1000 Python Questions
Get 1 Python question daily. Join this telegram channel https://t.me/python1000questions
Advertise with us
templates django   0   6003
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 the app then bundle them inside that app, otherwise, you may create a new app and then add the newly created app to INSTALLED_APPS.


Steps to create custom template tags:
  • Create a new directory inside your app and name it 'templatetags'.
  • Create the __init__.py  file inside this newly-created directory.
  • Create tags file. Lets name is custom_template_tag.py.
  • 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 the file, after import statements write register = template.library() .
  • Now let's create a very simple tag to get a random number in the template. For this, you need to import randint.
  • write code to generate a random number and register that function with the library. The complete code should look like below.

from django import template
from random import randint


register = template.Library()


@register.simple_tag
def random_number():
    return randint(0,999)
 


How to use a custom tag in Django templates:

Now when we have created a custom tag and registered it with the 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 anywhere 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 use to anywhere in the template. {% random_number as rnd %}. Now anywhere in template use {{rnd}} .



Things to take care of to make custom tags work:

  • Make sure you restarted the development server.
  • The directory name must be templatetags.
  • __init__.py  must be present in the directory.
  • App, where the templatetags directory is created, must be present in installed apps.
  • Load the tag file in the template file where the 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.

templates django   0   6003

Related Articles:
How to reset Django superuser password
This article explains 3 methods to reset the user password in Django, What command should be used to reset the superuser password from the terminal in Django application, Changing the user password in Django...
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, Django security...
How to upload an Image file in Django
This article explains the simple steps of uploading and storing an image in Django application, After storing the image, how to use it in Django template or emails, Uploading a file in Django, Storing image in Django model, Uploading and storing the image in Django model, HTML for multipart file upload...
Getting query params from request in Django
In this article, we will see how to access the query parameters from a request in the Django view, Accessing GET attribute of request, get() vs getlist() method of request in Django, query parameters Django,...

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


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

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap