Understanding Django's template language features
Let's start with a quick primer of Django Template Language (DTL) features.
Variables
Each template gets a set of context variables. Like Python's string format()
method's single curly brace {variable}
syntax, Django uses the double curly brace {{ variable }}
syntax. Let's see how they compare:
In pure Python, the syntax is <h1>{title}</h1>
. For example:
>>> "<h1>{title}</h1>".format(title="SuperBook")'<h1>SuperBook</h1>'
The syntax equivalent in a Django template is <h1>{{ title }}</h1>
. Rendering with the same context will produce the same output as follows:
>>> from django.template import Template, Context>>> Template("<h1>{{ title }}</h1>").render(Context({"title": "SuperBook"}))'<h1>SuperBook</h1>'
Attributes
Dot is a multipurpose operator in Django templates. There are three different kinds of operations: attribute lookup, dictionary lookup, or...