Django Tutorial · Django Syntax

Django Tags

Learn all about Django Tags in this comprehensive tutorial.

5 min read intermediate
  • In Django templates, you can perform programming logic like executing if statements and for loops.
  • The template tags are a way of telling Django that here comes something else than plain HTML.
  • A list of all template tags:

Template Tags

In Django templates, you can perform programming logic like executing if statements and for loops.

These keywords, if and for, are called "template tags" in Django.

To execute template tags, we surround them in {% %} brackets.

Django Code

The template tags are a way of telling Django that here comes something else than plain HTML.

The template tags allows us to to do some programming on the server before sending HTML to the client.

In the next chapters you will learn about the most common template tags.

Tag Reference

A list of all template tags:

TagDescription
autoescapeSpecifies if autoescape mode is on or off
blockSpecifies a block section
commentSpecifies a comment section
csrf_tokenProtects forms from Cross Site Request Forgeries
cycleSpecifies content to use in each cycle of a loop
debugSpecifies debugging information
extendsSpecifies a parent template
filterFilters content before returning it
firstofReturns the first not empty variable
forSpecifies a for loop
ifSpecifies a if statement
ifchangedUsed in for loops. Outputs a block only if a value has changed since the last iteration
includeSpecifies included content/template
loadLoads template tags from another library
loremOutputs random text
nowOutputs the current date/time
regroupSorts an object by a group
resetcycleUsed in cycles. Resets the cycle
spacelessRemoves whitespace between HTML tags
templatetagOutputs a specified template tag
urlReturns the absolute URL part of a URL
verbatimSpecifies contents that should not be rendered by the template engine
widthratioCalculates a width value based on the ratio between a given value and a max value
withSpecifies a variable to use in the block

Module quiz

2 questions
1

Which of the following is true about Django Tags?

2

What is the most common pitfall when working with Django Tags?

Answer all questions to submit.