DEV Community

Anurag Rana
Anurag Rana

Posted on

5 lesser used Django template tags

Originally published at pythoncircle.com.

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.

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.

Originally published at pythoncircle.com.

Top comments (1)

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

Wow this is good stuff I really didn't know about it.