Django for ISchoolers: Difference between revisions

no edit summary
imported>Aldeka
No edit summary
imported>Aldeka
No edit summary
Line 376:
 
== views.py ==
 
* Start the development server: <code>python manage.py runserver</code>
* 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/
 
Tried index in module qandabear.views. Error was: 'module'
object has no attribute 'index'</code>
 
Recall this line: <code>(r'^questions/$', 'qandabear.views.index')</code>.
 
* Explore this using your django-shell: <code>python manage.py shell</code>
 
<code>>>> import qandabear # imports fine!<br>
>>> import qandabear.views # imports fine also!<br>
>>> dir(qandabear.views) # what is in there!<br>
>>> 'index' in dir(qandabear.views)<br>
False<br>
>>> import inspect<br>
>>> inspect.getsourcefile(qandabear.views)<br>
# something like<br>
'/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.
 
Well, I guess we should do that!
 
* Write some views. Open polls/views.py and put the following Python code in it:
 
<code>from django.http import HttpResponse
 
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")</code>
 
This is a very simple view.
 
* Save the views.py file, then go to [http://127.0.0.1:8000/polls/ http://127.0.0.1:8000/polls/] in your browser, and you should see that text.
 
* Add a few more views by adding to the views.py file. These views are slightly different, because they take an argument (which, remember, is passed in from whatever was captured by the regular expression in the URLconf):
 
== Databases and the ORM (or: finally something that HTML/CSS/JS couldn't do for you on its own) ==
Anonymous user