Syntax and Filters

Two kinds of marker. {{ }} prints a value. {% %} is a tag that does something: a loop, a condition, extending a layout.

A filter transforms a value on the way out: {{ post.created|date:"j F Y" }}.

LIVE PREVIEW
loading editor…

This is the HTML a template produces. Templates are ordinary HTML with values dropped in, which is why editing them feels like editing a page rather than writing a program.

Your turn → Change the markup and watch the preview. Whatever you build here is what a template would render.

Templates are deliberately limited. You cannot call a function with arguments. You cannot do arbitrary Python.

This is not an oversight. It is a boundary: logic belongs in the view, where it can be read, reused and tested. When you find yourself fighting the template language, the answer is almost always to compute the value in the view and pass it in.

✦ {% empty %} exists and people miss it

A for loop can carry an {% empty %} block that renders when there is nothing to loop over. It saves a separate if, and it means the empty case is written right next to the loop where it will actually be maintained.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Inheritance and Escaping →
← Back