Skip to content

Advanced templates

Configuration

The settings.py file contains the template engine configuration:

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        'django.template.context_processors.debug',
        'django.template.context_processors.request',
        'django.contrib.auth.context_processors.auth',
        'django.contrib.messages.context_processors.messages',
      ],
    },
  },
]

The individual settings are used to configure:

  • BACKEND - engine (another option is e.g. Jinja)

  • DIRS - paths to search for template files

  • APP_DIRS - information whether templates directories in application folders are to be searched

  • OPTIONS -> context_processors - special objects in context

Tags

Place the marker in the brackets

  • {% tagname <tag_elements> %}

Some tags appear as "blocks"

  • {% tagname <tag_elements> %}
    block content
    {% endtagname %}

Documentation for the embedded tags can be found at https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#built-in-tag-reference.

Commonly used "non-block" tags:

  • Token CSRF:

    • {% csrf_token %}
  • Template inheritance

    • {% extends path_to_pattern_file %}
  • Template composition

    • {% include path_to_pattern_file %}

    • {% include path_to_pattern_file with **kwargs %}

  • Load library/application

    • {% load *library_names %}

      • ATTENTION! These are not strings! It's kind of like a Python import!
  • Relative URL to the view in the app

    • {% url *args **kwargs %}

    • {% url *args **kwargs as variable_name %}

  • Extracting path to static file (must be {% load static %})

    • {% static relative_path_of_static_files %}

"Block" imperative tags

  • Loops

    • {% for element in collection %}
      {% comment "required part" %}{% endcomment %}
      content to be repeated
      {% empty %}
      {% comment "optional part" %}{% endcomment %}
      content when collection is empty
      {% endfor %}
  • Conditions

    • {% if condition0 %}
      {% comment "required part" %}{% endcomment %}
      optional content
      {% elif condition1 %}
      {% comment "optional part" %}{% endcomment %}
      other optional content
      ...
      {% elif conditionn %}
      {% comment "optional part" %}{% endcomment %}
      another content
      {% else %}
      {% comment "optional part" %}{% endcomment %}
      alternative content
      {% endif %}

Often used „normal” „block” tags

  • Comments

    • {% comment "optional description of what is commented out" %}
      commented out content
      {% endcomment %}
  • Blocks

    • {% block name %}content{% endblock %}
  • Create variables

    • {% with **kwargs %}variables can be used here{% endwith %}

Variables

Place the variables in curly braces

  • {{ variable_name }}

We achieve nested structures only using a dot (we do not use square brackets)

  • {{ object.field }}

  • {{ object.method_without_arguments }}

  • {{ dictionary.key }}

  • {{ list.index }}

We can penetrate any deeply nested structures

  • {{ variable.deep.deeper }}

Filters

We can apply filters to variables. Filters are functions that we perform on objects. In addition to the filtered object, filters can receive only one argument.

The syntax of the filters is as follows:

  • {{ variable|filter_name:filter_argument }}

Filters documentation can be found at https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#built-in-filter-reference.

Frequently used filters

  • upper

  • lower

  • escape

  • escapejs

  • floatformat

  • join

  • first

  • last

  • length

  • linebreaksbr

  • pluralize

  • safe

  • slice

  • slugify

  • title

  • urlencode

  • i18n

    • {% load i18n %}
  • l10n

    • {% load l10n %}
  • tz

    • {% load tz %}