Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
templates tag   0   10507
5 lesser used Django template tags


We already know how to use for, if-else, filter and url template tags in Django. We can also create custom template tags in Django if any requirement is not getting fulfilled with existing tags. 


Here we are introducing you with 5 Django template tags which are lesser-known and used by beginner Django developers.


1. lorem

This template tag is used to insert random lorem ipsum Latin text in the template. This is useful when you want to show the sample data instead of blank space.

This tag accepts 3 optional parameters: count, method, and random.

The count is the number of words or paragraphs to produce. The method can have values w for words, p for HTML paragraphs and b for plain-text paragraphs. When the third parameter, random, is used, the text is generated randomly instead of using lorem ipsum text.

Refer to this Django template fiddle for example and demo.


2. templatetag

Double curly braces are used to display variables in Django templates. What if you want to display just curly braces in template. For this to achieve we can use templatetag template tag.


3. cycle

This tag accepts the variable number of arguments and outputs the next argument each time this tag is called. Once all arguments have been called, loop restarts from the starting.

We can use variables or string or a mix of both as arguments for this tag.

Refer to this Django template fiddle for example and demo. 

<!-- mylist is {"mylist":["Line 1","Line 2","Line 3","Line 4","Line 5","Line 6"]} -->
{% for item in mylist %}
    <div style="color:{% cycle 'red' 'green' 'blue' 'yellow' %}">
        {{item}}
    </div>
{% endfor %}


Output:

cycle tag in django template tags


4. firstof

This template tag accepts the variable number of arguments and returns the first argument which is not False i.e. which is not zero, empty string or False

Refer to this Django template fiddle for example and demo. 


5. phone2numeric

Converts any phone number to the numeric equivalent even if the phone number is not valid.

It will not convert any integer or boolean values.

Refer to this Django template fiddle for example and demo. 



templates tag   0   10507
0 comments on '5 Lesser Used Django Template Tags'

Related Articles:
Using IF ELSE condition in Django template
how to use IF ELSE in Django template, Syntax of IF, ELIF and ELSE in Django, Using filters within IF condition in Django template, Multiple elif (else if) confitions in Django template...
How to display flash messages in Django templates
flash messages in Django template, one-time notifications in Django template, messages framework Django, displaying success message in Django, error message display in Django...
Django Template Fiddle Launched !!!!
Django template fiddle, Experimenting with Django templates, Playing with Django template, Django fiddle, template fiddle, djangotemplatefiddle.com,...
Solving Django Error: TemplateDoesNotExist at /app_name/
How to solve TemplateDoesNotExist error in Django projects, reason for TemplateDoesNotExist error in Django, fixing TemplateDoesNotExist error in Django...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap