Skip to content

Commit

Permalink
Merge pull request #146 from pythonsd/rise-items
Browse files Browse the repository at this point in the history
Address some issues
  • Loading branch information
macro1 committed Nov 5, 2014
2 parents cd419e1 + 58f3643 commit 4477f80
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions myblog/blog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Meta:

def __init__(self, *args, **kwargs):
self.entry = kwargs.pop('entry') # the blog entry instance
super(CommentForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def save(self):
comment = super(CommentForm, self).save(commit=False)
comment = super().save(commit=False)
comment.entry = self.entry
comment.save()
return comment
30 changes: 24 additions & 6 deletions tutorials/02-models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Let's create an app for blog entries and related models. We'll call the app ``b
$ python manage.py startapp blog
This command should have created a ``blog`` directory with the following files::
This command should have created a ``blog`` directory with the following files and a subdirectory, migrations::

admin.py
__init__.py
admin.py
migrations
models.py
tests.py
views.py
migrations/__init__.py

We'll be focusing on the ``models.py`` file below.

Expand Down Expand Up @@ -66,8 +66,13 @@ Before we can use our app we need to add it to our ``INSTALLED_APPS`` in our set
Creating a model
----------------

First let's create a blog entry model. Models are objects used to interface with your data, and are described in the `Django model documentation`_.
This will correspond to a database table which will hold our blog entry. A blog entry will be represented by an instance of our ``Entry`` model class and each ``Entry`` model instance will identify a row in our database table.
First, let's create a blog entry model by writing the code below in our
`blog/models.py` file. Models are objects used to interface with your
data, and are described in the `Django model documentation`_. Our model
will correspond to a database table which will hold the data for our
blog entry. A blog entry will be represented by an instance of our
``Entry`` model class and each ``Entry`` model instance will identify a
row in our database table.

.. _Django model documentation: https://docs.djangoproject.com/en/1.7/topics/db/models/

Expand Down Expand Up @@ -145,7 +150,11 @@ Our blog entry was created
Our first test: __str__ method
----------------------------------

In the admin change list our entries all have the unhelpful name *Entry object*. We can customize the way models are referenced by creating a ``__str__`` method on our model class. Models are a good place to put this kind of reusable code that is specific to a model.
In the admin change list our entries have the unhelpful title
*Entry object*. Add another entry just like the first one, they will
look identical. We can customize the way models are referenced by
creating a ``__str__`` method on our model class. Models are a good
place to put this kind of reusable code that is specific to a model.

Let's first create a test demonstrating the behavior we'd like to see.

Expand Down Expand Up @@ -200,6 +209,11 @@ Now we're ready to create a real test.
.. _unittest: http://docs.python.org/2.7/library/unittest.html
.. _Testing Django applications: https://docs.djangoproject.com/en/1.7/topics/testing/overview/

.. NOTE::
`django.test.TestCase` extends the `unittest.TestCase` class.
Anything you would do in the base `unittest` class will work in
Django's `TestCase` as well.

Let's write our test to ensure that a blog entry's string representation is equal to its title. We need to modify our tests file like so:

.. code-block:: python
Expand Down Expand Up @@ -262,6 +276,8 @@ Let's add a ``__str__`` method to our model that returns the entry title. Our `
If you start the development server and take a look at the admin interface (http://localhost:8000/admin/) again, you will see the entry titles in the list of entries.

.. image:: _static/02-04_entry_w_name.png

Now if we run our test again we should see that our single test passes:

.. code-block:: bash
Expand Down Expand Up @@ -290,6 +306,8 @@ Another Test: Entrys

Did you notice that the pluralization of entry is misspelled in the admin interface? "Entrys" should instead read "Entries". Let's write a test to verify that when Django correctly pluralizes "entry" to "entries".

.. image:: _static/02-05_entrys_spelling.png

Let's add a test to our ``EntryModelTest`` class:

.. code-block:: python
Expand Down
5 changes: 4 additions & 1 deletion tutorials/03-views.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ Now we need to route the homepage URL to the home view. Our URL file ``myblog/u
]
Now let's visit http://localhost:8000/ in a web browser to check our work. You should see a webpage that looks like this:
Now let's visit http://localhost:8000/ in a web browser to check our
work. (Restart your server with the command
`python manage.py runserver`.) You should see a webpage that looks like
this:

.. image:: _static/03-01_myblog.png

Expand Down
7 changes: 5 additions & 2 deletions tutorials/04-more-views.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ gets synced to our SQLite database.
Applying blog.0002_auto_20141019_0232... OK
Let's write a ``__str__`` method for our ``Comment`` model like we did for our ``Entry`` model earlier.
Add a ``__str__`` method for our ``Comment`` model, similar to the one
we previously added for our ``Entry`` model.

First we should create a test in ``blog/tests.py``. Our test should look very similar to the ``__str__`` test we wrote for entries earlier. This should suffice:
Now we need to create a test in ``blog/tests.py``. Our test should look
very similar to the ``__str__`` test we wrote in ``EntryModelTest``
earlier. This should suffice:

.. code-block:: python
Expand Down
4 changes: 2 additions & 2 deletions tutorials/05-forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ Okay now let's finally write our form code.
def __init__(self, *args, **kwargs):
self.entry = kwargs.pop('entry') # the blog entry instance
super(CommentForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def save(self):
comment = super(CommentForm, self).save(commit=False)
comment = super().save(commit=False)
comment.entry = self.entry
comment.save()
return comment
Expand Down
Binary file modified tutorials/_static/02-03_entry_added.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tutorials/_static/02-04_entry_w_name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tutorials/_static/02-05_entrys_spelling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4477f80

Please sign in to comment.