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>'

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.