Template Cheats

This section covers the common operations performed within the django templating language. The table lists the common operations and examples are given below.


Please note that this is site provides the most commonly used django features. It is by no means intended to be an exhaustive documentation of the Django framework. You can always use the official django documentation by clicking the big green button below.

Django Templating Language Docs

Django Templates

Django Template Operation Functionality
{{ }} Allows data to be displayed
{{ <variable_name> }} An example of how to display data
{{ <varibale_name|<filter_name> }} Modifies the output of a variable using a chosen filter
{% %} Allows logical operations to be executed
{% extends '<a_file_name>' %} Permits templating from a 'base.html' file
{% include '<a_partial_file_name>' %} Permits partial html files to be used where this is declared
{% block <block_name> %} Begins a new block
{% endblock %} Declares the end of a block
{% if %} Begins an if statement
{% endif %} Ends an if statement
{% for %} Begins a for loop
{% endfor %} Ends a for loop
{% empty %} Allows for loops to display nothing if the item list is empty
{% for item in items %} <p>{{ item }}</p> {% endfor %} An example of a for loop
{% with <var_name>=<complex_var> %} Allows for a variable to be assigned
{% endwith %} Ends an with block
{% now %} Displays the current datetime
{% lorem %} Outputs the commonly used 'lorem ipsum' text
{% lorem <number> p %} Outputs latin text using a specified number of <p> tags
{% lorem <number> w random %} Outputs a specified number random latin words
{% comment <note> %} Forces everything to be ignored after this tag
{% endcomment %} Ends a comment
{% cycle <var1> <var2... %} Produces one of the variables each time it is cycle. Used inside <code class='snipp'>{% for %}</code> tag
{% autoescape <on/off> %} Turns on or off autoescape of html elements. The default is on
{% endautoescape %} Ends autoescaping of html

Django Filters

Django Template Operation Functionality
{{ <var>|add:'<number>' }} Add's a number to a variable
{{ <var>|capfirst }} Capitalizes the first letter of a variable
{{ <var>|title }} Capitalizes the first letter of a all words of a string, like a title
{{ <var>|default:'<default_display_value>' }} Permits a value to be displayed ir a variable does not exist
{{ <var>|addslahses }} Adds slahses before quotes