DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on • Edited on

Six Django template tags not often used in tutorials

This article is for those who don't read the documentation, and I, who had the Dash app for a few months now, which I never tinkered until last night.

During my first day on my internship a couple of months back, I was tasked to work on the scaffold of the company on which I was overwhelmed with the tags on it and never really bothered to research them.

Some of these are taken from the scaffold, some not.

Side note: There are spaces on the template tag because I have not figured out how to make the tags work on this site.

1.) for...empty

  • The for tag can take an optional {% empty %} clause whose text is displayed if the given array is empty or could not be found:
{ % for student in student_list % }
    ...
{ % empty % }
    ...
{ % endfor % }
Which is also equivalent to:

  { % if student_list % }
    { % for student in student_list % }
      ...
    { % endfor % }
  { % else % }
    ...
  { % endif % }

2.) lorem

  • No, you don't need any other packages nor copy/paste a lorem text. This tag displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates. Unless, of course, you don't.
{ % lorem [count] [method] [random] % }

e.g.

- { % lorem % } will output the common “lorem ipsum” paragraph.
- { % lorem 3 p % } will output the common “lorem ipsum” paragraph and two random paragraphs each wrapped in HTML 

tags. - { % lorem 2 w random % } will output two random Latin words.

3.) now

  • Displays the current date and/or time, using a format according to the given string. Such string can contain format specifiers characters as described in the date filter section.
{ % now "jS F Y" % }

4.) resetcycle

  • Resets a previous cycle so that it restarts from its first item at its next encounter. Without arguments, {% resetcycle %} will reset the last {% cycle %} defined in the template.
{ % for coach in coach_list % }
      { { coach.name } }
    { % for athlete in coach.athlete_set.all % }
        

{ { athlete.name } }

{ % endfor % } { % resetcycle % } { % endfor % }

This example would return this HTML:

José Mourinho

Thibaut Courtois

John Terry

Eden Hazard

Carlo Ancelotti

Manuel Neuer

Thomas Müller

5.) verbatim

  • Stops the template engine from rendering the contents of this block tag.

  • A common use is to allow a JavaScript template layer that collides with Django’s syntax. For example:

{ % verbatim % }
    { {if dying} }Still alive.{ {/if} }
{ % endverbatim % }
  • You can also designate a specific closing tag, allowing the use of { % endverbatim % } as part of the unrendered contents:
    { % verbatim myblock % }
    Avoid template rendering via the { % verbatim % }{ % endverbatim % } block.
    { % endverbatim myblock % }
    

6.) widthratio

  • For creating bar charts and such, this tag calculates the ratio of a given value to a maximum value, and then applies that ratio to a constant.
Imagine an image here
  • If this_value is 175, max_value is 200, and max_width is 100, the image in the above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).

  • In some cases, you might want to capture the result of the width ratio in a variable. It can be useful, for instance, in a blocktrans like this:

{ % widthratio this_value max_value max_width as width % }
{ % blocktrans % }The width is: { { width } }{ % endblocktrans % }

Final side note: There are spaces on the template tag because I have not figured out how to make the tags work on this site.

Buy Me A Coffee

Top comments (4)

Collapse
 
hundredrab profile image
100

Great post! BTW, you can use braces here like this


```html
{% widthratio this_value max_value max_width as width %}
{% blocktrans %}The width is: {{ width }}{% endblocktrans %}
```

Which will render as expected:

{% widthratio this_value max_value max_width as width %}
{% blocktrans %}The width is: {{ width }}{% endblocktrans %}
Collapse
 
lewiskori profile image
Lewis kori

Nice. I didn't know all of this this existed with django. Thanks for the info.

Collapse
 
davidmm1707 profile image
David MM👨🏻‍💻

I didn't know about 'resetcycle' and 'widthratio' tag. Great post.

P.S: Edit your 6th point title.

Collapse
 
highcenburg profile image
Vicente G. Reyes

Believe me theres a lot! Dash makes it easy for developers when they segregated the categories unlike what you see on the docs