Django for ISchoolers: Difference between revisions

Content added Content deleted
imported>Aldeka
No edit summary
imported>Aldeka
No edit summary
Line 403: Line 403:
Well, I guess we should do that!
Well, I guess we should do that!


* Write some views. Open polls/views.py and put the following Python code in it:
* Write some views. Open qandabear/views.py and put the following Python code in it:


<code>from django.http import HttpResponse
<code>from django.http import HttpResponse


def index(request):
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")</code>
return HttpResponse("Hello, world. You're at the questions index.")</code>


This is a very simple view.
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.
* Save the views.py file, then go to [http://127.0.0.1:8000/questions/ http://127.0.0.1:8000/questions/] 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):
* Add a few more views by adding to the views.py file. These views are slightly different, because they take an argument or two (which, remember, is passed in from whatever was captured by the regular expression in the URLconf):

<code>def question(request, question_id):
return HttpResponse("You're looking at question %s." % (question_id,))

def edit_answer(request, question_id, answer_id):
return HttpResponse("You're looking at the edit page for answer %s." % (answer_id,))</code>

* Save views.py.

* Navigate to [http://127.0.0.1:8000/questions/34/ http://127.0.0.1:8000/questions/34/]. It’ll run the question() method and display whatever ID you provide in the URL.
* Add a little html to the ‘question’ view. Wrap the question_id in <strong> </strong> tags and verify that the view is indeed bold!
* Add and commit your code. Remember to write a good commit message that mentioned what changed (in English) and more details below.


== Databases and the ORM (or: finally something that HTML/CSS/JS couldn't do for you on its own) ==
== Databases and the ORM (or: finally something that HTML/CSS/JS couldn't do for you on its own) ==