Boston Python Workshop/Saturday/Web app project: Difference between revisions

Revert to pre-spam
imported>Paulproteus
(Revert to pre-spam)
 
(22 intermediate revisions by 12 users not shown)
Line 8:
 
This is based ''heavily'' on the official tutorial for the Django web programming framework.
 
This page should say what you should actually expect to know. It is okay that you don't understand everything you are typing in. After a lot more learning, you will be able to. The first time, though, it's okay if you don't. Will and Katie have feedback for this page.
 
== Writing your first Django app, part 1 ==
Line 551 ⟶ 553:
return HttpResponse(output)
 
Now go to "http://localhost:8000/polls/" in your Web browser. You should see the text of the first poll. There's a problem here, though: The page's design is hard-coded in the view. If you want to change the way the page looks, you'll have to edit this Python code. So let's use Django's template system to separate the design from Python:
 
from django.shortcuts import render_to_response
Line 598 ⟶ 600:
</pre>
 
Load the page in"http://localhost:8000/polls/" into your Web browser again, and you should see a bulleted-list containing the "What's up" poll from Tutorial 1. The link points to the poll's detail page.
 
=== Raising 404 ===
 
Now, let's tackle the poll detail view -- the page that displays the question for a given poll. Continue editing the ''views.py'' file. This view uses Python ''exceptions'':
 
from django.http import Http404
Line 615 ⟶ 617:
The new concept here: The view raises the Http404 exception if a poll with the requested ID doesn't exist.
 
If you'd like to quickly get the above example working, just create a new template file and name it ''detail.html''. Enter in it just one line of code:
 
<pre>
Line 621 ⟶ 623:
</pre>
 
willto get you started for now.
 
Does your detail view work? Try it: http://127.0.0.1:8000/polls/1/
Add it to a new template file that you create, ''detail.html''.
 
DoesYou yourcan detailalso viewtry work?to Tryload ita poll page that does not exist, just to test out the pretty 404 error: http://127.0.0.1:8000/polls/detail/132/
 
=== Adding more detail ===
Line 645 ⟶ 647:
 
Method-calling happens in the {% for %} loop: poll.choice_set.all is interpreted as the Python code poll.choice_set.all(), which returns a sequence of Choice objects and is suitable for use in the {% for %} tag.
 
Load the new detail page in your browser: http://127.0.0.1:8000/polls/1/ The poll choices now appear.
 
=== Adding some style ===
Line 679 ⟶ 683:
* Since we're creating a POST form (which can have the effect of modifying data), we need to worry about Cross Site Request Forgeries. Thankfully, you don't have to worry too hard, because Django comes with a very easy-to-use system for protecting against it. In short, all POST forms that are targeted at internal URLs should use the {% csrf_token %} template tag.
 
The {% csrf_token %} tag requires information from the request object, which is not normally accessible from within the template context. To fix this, a small adjustment needs to be made to the detail view in the "views.py" file, so that it looks like the following:
 
<pre>
Line 690 ⟶ 694:
</pre>
 
The details of how this works are explained in the [http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext documentation for RequestContext].
 
Now, let's create a Django view that handles the submitted data and does something with it. Remember, in Tutorial 3, we created a URLconf for the polls application that includes this line:
Line 797 ⟶ 801:
Okay, not quite finally. You might need to go to https://admin.alwaysdata.com/advanced/processes/ and click ''Restart my applications''.
 
Go to your alwaysdata site's /adminpolls/ page. For me, I'd go to:
 
* http://paulproteus.alwaysdata.com/adminpolls/
 
You should see your poll!
Create a poll! Create some choices. Find your views, and show them to the world.
 
== Part 4: Editing your polls in the Django admin interface ==
Line 965 ⟶ 969:
 
That's the basics of the Django admin interface!
 
Create a poll! Create some choices. Find your views, and show them to the world.
 
== Part 4.5: Deploy again, again! ==
Anonymous user