Permalink
Browse files

Add filename captions to remaing source code blocks

  • Loading branch information...
1 parent 0fdc4ce commit b9920840695a837747e602715a95e24776cd2dfd @hjwp committed Mar 4, 2012
Showing with 128 additions and 36 deletions.
  1. +22 −2 tutorial02.rst
  2. +21 −3 tutorial03.rst
  3. +58 −31 tutorial04.rst
  4. +27 −0 tutorial05.rst
View
@@ -33,9 +33,10 @@ Login with the admin username and password (``admin / adm1n``).
If you go into the Polls section and try and create a new Poll, you need
to click on a link that says "Add Poll" - let's add that to our FT. Delete
-the "TODO", then add the following, in ``polls/test_amin.py``:
+the "TODO", then add the following, in ``fts/test_amin.py``:
.. sourcecode:: python
+ :filename: mysite/fts/test_admin.py
# She now sees a couple of hyperlink that says "Polls"
polls_links = self.browser.find_elements_by_link_text('Polls')
@@ -66,6 +67,7 @@ So that's one thing we'll want to change. Let's add a test for that to the end
our FT
.. sourcecode:: python
+ :filename: mysite/fts/test_admin.py
# She sees some input fields for "Question" and "Date published"
body = self.browser.find_element_by_tag_name('body')
@@ -108,6 +110,7 @@ Google Chrome), you'll find out that the 'name' for our three fields are
.. sourcecode:: html
+ :filename: html source for admin site
<label for="id_question" class="required">Question:</label>
<input id="id_question" type="text" class="vTextField" name="question" maxlength="200" />
@@ -126,6 +129,7 @@ Google Chrome), you'll find out that the 'name' for our three fields are
Let's use them in our FT
.. sourcecode:: python
+ :filename: mysite/fts/test_admin.py
# She sees some input fields for "Question" and "Date published"
body = self.browser.find_element_by_tag_name('body')
@@ -147,6 +151,7 @@ Let's use them in our FT
We can also use the CSS selector to pick up the "Save" button
.. sourcecode:: python
+ :filename: mysite/fts/test_admin.py
save_button = self.browser.find_element_by_css_selector("input[value='Save']")
save_button.click()
@@ -162,6 +167,7 @@ Django lets you give them more descriptive names, including any attribute of
the object. So let's say we want our polls listed by their question
.. sourcecode:: python
+ :filename: mysite/fts/test_admin.py
# She is returned to the "Polls" listing, where she can see her
# new poll, listed as a clickable link
@@ -202,9 +208,10 @@ Unit testing the verbose name for pub_date
Django stores human-readable names for model attributes in a special attribute
called `verbose_name`. Let's write a unit test that checks the verbose name
-for our ``pub_date`` field. Add the following method to ``polls\tests.py``
+for our ``pub_date`` field. Add the following method to ``polls/tests.py``
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
def test_verbose_name_for_pub_date(self):
for field in Poll._meta.fields:
@@ -229,6 +236,7 @@ Now that we have a unit test, we can implement! Let's make a change in
``models.py``
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Poll(models.Model):
question = models.CharField(max_length=200)
@@ -257,6 +265,7 @@ happens in the ``__unicode__`` method. As usual, we unit test first, in this
case it's a very simple one -
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
def test_poll_objects_are_named_after_their_question(self):
p = Poll()
@@ -280,6 +289,7 @@ And the fix is simple too - we define a ``__unicode__`` method on our Poll class
in ``models.py``
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Poll(models.Model):
question = models.CharField(max_length=200)
@@ -328,6 +338,7 @@ So let's add that as an intermediate step in our FT, in between where
Florence enters the question, and when she hits save.
.. sourcecode:: python
+ :filename: mysite/fts/test_admin.py
[...]
time_field.send_keys('00:00')
@@ -358,6 +369,7 @@ admin page, because there's no such thing yet! Let's go ahead and create our
"Choice" model then. As usual, we start with some unit tests - in ``polls/tests.py``
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
class TestPollChoicesModel(TestCase):
@@ -397,12 +409,14 @@ admin page, because there's no such thing yet! Let's go ahead and create our
Also remember to add the import to the top of the file
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
from polls.models import Choice, Poll
And we may as well give it something to import too - in ``polls/models.py``
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Choice(object):
pass
@@ -461,6 +475,7 @@ https://docs.djangoproject.com/en/1.3/intro/tutorial01/#playing-with-the-api
Let's add that relationship now
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Choice(models.Model):
poll = models.ForeignKey(Poll)
@@ -480,6 +495,7 @@ Re-running the unit tests, we get::
Let's give Choice a choice...
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Choice(models.Model):
poll = models.ForeignKey(Poll)
@@ -492,6 +508,7 @@ Tests again::
Let's add votes
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Choice(models.Model):
poll = models.ForeignKey(Poll)
@@ -541,6 +558,7 @@ Let's edit ``polls/admin.py``, and do some customising on the way the Poll
admin page works
.. sourcecode:: python
+ :filename: mysite/polls/admin.py
from django.contrib import admin
from polls.models import Choice, Poll
@@ -580,6 +598,7 @@ may need to ``syncdb``)
Let's make 'votes' default to 0, by adding a new test in ``tests.py``
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
def test_choice_defaults(self):
choice = Choice()
@@ -592,6 +611,7 @@ And run it::
And set the default, in ``polls/models.py``
.. sourcecode:: python
+ :filename: mysite/polls/models.py
class Choice(models.Model):
poll = models.ForeignKey(Poll)
View
@@ -1,3 +1,6 @@
+Part 3 - A normal web page, using Django views and templates
+============================================================
+
Welcome to part 3 of the tutorial! This week we'll finally get into writing
our own web pages, rather than using the Django Admin site. Here's a summary
of what we'll get up to:
@@ -9,9 +12,6 @@ of what we'll get up to:
* Use the Django Test Client to write unit tests for the above
-Part 3 - A normal web page, using Django views and templates
-==========================================================
-
Let's pick up our FT where we left off - we now have the admin site set up to
add Polls, including Choices. We now want to flesh out what the user sees.
@@ -24,6 +24,7 @@ the user's actions, and the expected behaviour of the site
Create a new file for it called ``fts/test_polls.py``.
.. sourcecode:: python
+ :filename: mysite/fts/test_polls.py
from functional_tests import FunctionalTest, ROOT
from selenium.webdriver.common.keys import Keys
@@ -75,6 +76,7 @@ info here:*
http://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python)
.. sourcecode:: python
+ :filename: mysite/fts/test_polls.py
from functional_tests import FunctionalTest, ROOT
from selenium.webdriver.common.keys import Keys
@@ -178,6 +180,7 @@ website, sees some polls and votes on them.
.. sourcecode:: python
+ :filename: mysite/fts/test_polls.py
def test_voting_on_a_new_poll(self):
# First, Gertrude the administrator logs into the admin site and
@@ -246,6 +249,7 @@ https://docs.djangoproject.com/en/1.3/topics/testing/
We'll create a new class to test our home page view:
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
from django.test.client import Client
[...]
@@ -274,13 +278,15 @@ our "404 error" page. We'll come back to that later. For now, let's make the
First we'll create a dummy view in ``views.py``:
.. sourcecode:: python
+ :filename: mysite/polls/views.py
def home(request):
pass
Now let's hook up this view inside ``urls.py``:
.. sourcecode:: python
+ :filename: mysite/polls/urls.py
from mysite.polls import views
@@ -321,6 +327,7 @@ Re-running our tests should show us a different error::
Let's get the view to return an HttpResponse:
.. sourcecode:: python
+ :filename: mysite/polls/views.py
from django.http import HttpResponse
@@ -346,6 +353,7 @@ be simple enough to just return a response that contained the questions of our
two polls as "raw" text - like this:
.. sourcecode:: python
+ :filename: mysite/polls/views.py
from django.http import HttpResponse
from polls.models import Poll
@@ -412,6 +420,7 @@ template, by using a special method response called ``assertTemplateUsed``,
so let's use that. In ``tests.py``:
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
class TestHomePageView(TestCase):
@@ -459,6 +468,7 @@ So let's now create our template::
Edit ``home.html`` with your favourite editor,
.. sourcecode:: html+django
+ :filename: mysite/polls/templates/home.html
<html>
<body>
@@ -481,6 +491,7 @@ Let's rewrite our code to use this template. For this we can use the Django
``render`` function, which takes the request and the name of the template:
.. sourcecode:: python
+ :filename: mysite/polls/views.py
from django.shortcuts import render
from polls.models import Poll
@@ -510,6 +521,7 @@ what context objects were used in rendering a response, so we can write a test
for that too:
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
client = Client()
response = client.get('/')
@@ -549,6 +561,7 @@ but make them empty - again, the idea is to make the minimal change to move
the test forwards:
.. sourcecode:: python
+ :filename: mysite/polls/views.py
def home(request):
context = {'polls': []}
@@ -577,6 +590,7 @@ Now the unit tests say::
Let's fix our code so the tests pass:
.. sourcecode:: python
+ :filename: mysite/polls/views.py
from django.shortcuts import render
from polls.models import Poll
@@ -698,6 +712,7 @@ be making a view for seeing an individual `Poll` object, so we'll probably find
the poll using its ``id``. Here's what that translates to in ``tests.py``:
.. sourcecode:: python
+ :filename: mysite/polls/tests.py
from django.core.urlresolvers import reverse
@@ -754,6 +769,7 @@ Capturing parameters from URLs
In ``urls.py``:
.. sourcecode:: python
+ :filename: mysite/polls/urls.py
urlpatterns = patterns('',
(r'^$', views.home),
@@ -769,6 +785,7 @@ reverse function's ``args`` parameter.
We'll also need to add at least a dummy view in ``views.py``
.. sourcecode:: python
+ :filename: mysite/polls/urls.py
def home(request):
context = {'polls': Poll.objects.all()}
@@ -792,6 +809,7 @@ Now our unit tests give a different error::
The templates don't include the urls yet. Let's add them:
.. sourcecode:: html+django
+ :filename: mysite/polls/templates/home.html
<html>
<body>
Oops, something went wrong.

0 comments on commit b992084

Please sign in to comment.