Django for ISchoolers: Difference between revisions

Content added Content deleted
imported>Aldeka
No edit summary
imported>Aldeka
No edit summary
Line 256: Line 256:
== Starting yer app ==
== Starting yer app ==


In this tutorial, we’ll create our poll app in the myproject directory for simplicity. In the future, when you decide that the world needs to be able to use your poll app and plug it into their own projects, and after you determine that your app plays nicely with other apps, you can publish that directory separately!
In this tutorial, we’ll create our Q&A app in the myproject directory for simplicity. In the future, when you decide that the world needs to be able to use your Q&A app and plug it into their own projects, and after you determine that your app plays nicely with other apps, you can publish that directory separately!


* Open your terminal and navigate into the mysite folder
* Open your terminal and navigate into the mysite folder
Line 365: Line 365:
* Save and commit.
* Save and commit.


Review: When somebody requests a page from your Web site – say, “/questions/23/”, Django will load the urls.py Python module, because it’s pointed to by the ROOT_URLCONF setting. It finds the variable named urlpatterns and traverses the regular expressions in order. When it finds a regular expression that matches – r'^polls/(\d+)/$' – it loads the function question() from qandabear/views.py. Finally, it calls that module’s detail() function like so:
Review: When somebody requests a page from your Web site – say, “/questions/23/”, Django will load the urls.py Python module, because it’s pointed to by the ROOT_URLCONF setting. It finds the variable named urlpatterns and traverses the regular expressions in order. When it finds a regular expression that matches – r'^questions/(\d+)/$' – it loads the function question() from qandabear/views.py. Finally, it calls that module’s detail() function like so:


<code>detail(request=<HttpRequest object>, '23')</code>
<code>detail(request=<HttpRequest object>, '23')</code>
Line 380: Line 380:
* Fetch “http://127.0.0.1:8000/questions/” in your browser. You should get a pleasantly-colored error page with the following message:
* Fetch “http://127.0.0.1:8000/questions/” in your browser. You should get a pleasantly-colored error page with the following message:


<code>ViewDoesNotExist at /polls/
<code>ViewDoesNotExist at /questions/


Tried index in module qandabear.views. Error was: 'module'
Tried index in module qandabear.views. Error was: 'module'
Line 399: Line 399:
'/Users/adalovelace/gits/mysite/questions/views.py'</code>
'/Users/adalovelace/gits/mysite/questions/views.py'</code>


So, a mystery? Where is the view!? It’s nowhere! The URL parsing is going fine, but there is no one listening at the other end of the phone! This ViewDoesNotExist error happened because you haven’t written a function index() in the module polls/views.py.
So, a mystery? Where is the view!? It’s nowhere! The URL parsing is going fine, but there is no one listening at the other end of the phone! This ViewDoesNotExist error happened because you haven’t written a function index() in the module questions/views.py.


Well, I guess we should do that!
Well, I guess we should do that!
Line 571: Line 571:
===Fix The Hideous Default Representation===
===Fix The Hideous Default Representation===


Wait a minute! <Question: Question object> is an utterly unhelpful, truly wretched, beyond contemptable representation of this object. Let’s fix that by editing the Question model. Use your text editor to open the polls/models.py file and adding a __unicode__() method to both Question and Answer:
Wait a minute! <Question: Question object> is an utterly unhelpful, truly wretched, beyond contemptable representation of this object. Let’s fix that by editing the Question model. Use your text editor to open the qandabear/models.py file and adding a __unicode__() method to both Question and Answer:


<code>class Question(models.Model):
<code>class Question(models.Model):