liveBook · Manning

Dotted notation

As explained in Django in Action

Dot notation is syntax that accesses attributes, collection items, dictionary values, or no-argument callables through period-separated names.

Dotted notation uses periods to reach values inside template data. In a Django template, the same form can access an object attribute, a dictionary value, a list or tuple item, or a no-argument callable. section 3.2 section 3.4

Common Django template lookups include:

  • {{ person.name }} reads the name attribute of an object or the name key of a dictionary. section 3.4
  • {{ fruit.2 }} reads the third item in a list or tuple; template notation uses a dot where Python would use square brackets. section 3.4
  • {{ person.show_name }} invokes show_name() without arguments and renders its result. section 3.4
  • {{ instrument.name }} and {{ instrument.cost }} expose fields while a {% for %} loop renders each instrument. section 3.5.2

Rendering instrument data

Given an iterable named instruments containing objects with name and cost attributes, a template can render each one as a list item: {% for instrument in instruments %}<li>{{ instrument.name }}: {{ instrument.cost }}</li>{% endfor %}. If the iterable is empty, an {% empty %} branch can display a fallback message. section 3.5.2

<ul>
    {% for instrument in instruments %}
        <li> {{instrument.name}}: {{instrument.cost}}</li>
    {% endfor %}
</ul>

See dotted notation operating inside a Django template loop that builds an HTML list. section 3.5.2

How Django in Action covers it

How deep it goes
Explains four lookup forms and demonstrates loop usage, but stops short of detailed lookup precedence or error behavior.
Examples
Uses person attributes, dictionary keys, fruit indexing, no-argument methods, and instrument lists rendered as HTML.
What it assumes
Assumes basic template variables and Python-like data structures while teaching their unified Django template lookup syntax.
What you can do after
Apply it via Django templates to display context data and fields while iterating over collections.
How it connects
Ties dotted notation to Django templates, context objects, template variables, and for loops.

Also explained in

Compare how 9 books explain dotted notation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.