Friday, March 25, 2016

Django tutorial part 3 - question_id vs question.id

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



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5a9ecd2b-5c6c-46d5-8cfe-46206126c6c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment