Django for ISchoolers: Difference between revisions

Content added Content deleted
imported>Aldeka
imported>Aldeka
Line 826: Line 826:
* Reload [http://127.0.0.1:8000/questions/ http://127.0.0.1:8000/questions/] . You should see a bulleted-list containing up to five of your questions. There should also be link pointing to the question's detail page.
* Reload [http://127.0.0.1:8000/questions/ http://127.0.0.1:8000/questions/] . You should see a bulleted-list containing up to five of your questions. There should also be link pointing to the question's detail page.


*Save and commit.
* Save and commit.

===Fix the detail view and handle user errors using a 404 ===

Now, let’s tackle the question detail view – the page that displays one single question.

* Edit the views.py file. This view uses Python exceptions:

<source lang="python">
from django.http import Http404
# ...
def detail(request, question_id):
try:
q = Question.objects.get(id=question_id)
except Question.DoesNotExist:
raise Http404
return render_to_response('qandabear/detail.html', {'question': q})
</source>

Notice that view raises the Http404 exception if a poll with the requested ID doesn’t exist.

* Create qandabear/templates/qandabear/detail.html with:

<source lang="python">
{{ question }}
</source>

* Verify your “detail” view works. Try it: [http://127.0.0.1:8000/questions/1/ http://127.0.0.1:8000/questions/1/]

* Try visiting [http://127.0.0.1:8000/questions/9000/ http://127.0.0.1:8000/questions/9000/]. Oops, we forgot to make a template for our lovely 404 error! Create qandabear/templates/404.html (the qandabear template root dir) as:

<source lang="html"><p>You have a 404. Go back and try again.</p></source>

* Load a question page that does not exist, to test out the pretty 404 error: [http://127.0.0.1:8000/questions/9000/ http://127.0.0.1:8000/questions/9000/]

* What? It says DEBUG has to be False? All right, set it (in settings.py), and try again! (note: Chrome ‘eats’ the 404. Safari will show our created page.)

* Change DEBUG back to True

* Save and commit.

=== Add more detail to the details! ===

Great, we've got a question detail page. But it isn't really that useful. We don't just want to see the question text -- we want to see the question's answers, too!

* Edit the qandabear/detail.html template to add a question variable. question points the particular instance of the Question class that our view sent to the template.

<source lang="python">
<h1>{{ question.text }}</h1>
<ul>
{% for answer in question.answer_set.all %}
<li>{{ answer.text }} -- <em>Answered at {{ answer.pub_date }} -- {{ answer.votes }} votes</em></li>
{% endfor %}
</ul>
</source>

The django.template system uses dot-lookup syntax to access variable attributes. Django’s template language is a bit looser than standard python. In pure Python, the . (dot) only lets you get attributes from objects, and we would need to use [] to access parts of list, tuple or dict objects. In this example, we are just doing attribute lookup, but in general if you’re not sure how to get data out of an object in django.templates, try dot.

Method-calling happens in the {% for %} loop: question.answer_set.all is interpreted as the Python code question.answer_set.all(), which returns a sequence of Answer objects and is suitable for use in the {% for %} template tag.

* Reload [http://127.0.0.1:8000/questions/1/ http://127.0.0.1:8000/questions/1/]. Observe that the question's answers now appear, with a little metadata!

* Save and commit your changes.


= Chunk 4 =
= Chunk 4 =