Reputation: 3576
I am going through the Django project tutorial, and in this section it says:
The template system uses dot-lookup syntax to access variable attributes. In the example of {{ question.question_text }}, first Django does a dictionary lookup on the object question. Failing that, it tries an attribute lookup – which works, in this case. If attribute lookup had failed, it would’ve tried a list-index lookup.
Does this mean that the Django question
is a dictionary object, and in the first instance, looks for question_text
as the key, and if found, returns the value? Beyond this, I can't visualise what the two fall-back options are doing.
Upvotes: 0
Views: 516
Reputation: 136919
Does this mean that the Django
question
is a dictionary object, and in the first instance, looks forquestion_text
as the key, and if found, returns the value? Beyond this, I can't visualise what the two fall-back options are doing.
question
doesn't have to be a literal dict
for the first option to work. It needs to be dictionary-like. That is, question['question_text']
works in Python.
The first fallback refers to regular Python dot notation. For example, if either of these works in Python:
question.question_text # or
question.question_text()
then question.question_text
will work in the template returning the Python value. Note that parentheses are omitted in both cases.
The final fallback is numeric indexing. For example, if question
is a list and this works in Python:
question[0]
then question.0
will work in the template, returning the value of question[0]
.
Upvotes: 7