Hello,
In trying to update my templates after I successfully finished part 3 of the tutorial, I found that I could write something like
<p>You're looking at the results of question {{ question.id }}: "{{ question.question_text }}"</p>
and get: You're looking at the results of question 1: "What's up?"
However, if I write
<p>You're looking at the results of question {{ question.question_id }}: "{{ question.question_text }}"</p>
I get: You're looking at the results of question : "What's up?"
I think the answer is that "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
." And "id" is an attribute of the question object. But if that's the case, then why is "question_id" used everywhere else (views.py, urls.py)? Is it because we *define* the question_id parameter by the pattern of the incoming request in views.py? In other words, if we changed ?P<question_id> to ?P<my_favorite_number>, then we would be writing our views like this:
def detail(request, my_favorite_number):
question = get_object_or_404(Question, pk=my_favorite_number)
but still referencing the id as {{
question.id }} in our templates?
Hmm, I think I might have answered my own question, but I definitely thought I had exhausted my resources before deciding to write this post. ; )
Thank you,
Kurt