Background Image
Table of Contents Table of Contents
Previous Page  138 / 478 Next Page
Information
Show Menu
Previous Page 138 / 478 Next Page
Page Background

Get FTs to clean up after themselves

Adjust model so that items are associated

with different lists

Add unique URLs for each list

Add a URL for creating a new list via POST

Add URLs for adding a new item to an

existing list via POST

Refactor away some duplication in urls.py

Irritatingly, the Testing Goat is a stickler for tying up loose ends too, so we’ve got to do

this one final thing.

Before we start, we’ll do a commit—always make sure you’ve got a commit of a working

state before embarking on a refactor:

$

git diff

$

git commit -am "new URL + view for adding to existing lists. FT passes :-)"

A Final Refactor Using URL includes

superlists/urls.py

is really meant for URLs that apply to your entire site. For URLs that

only apply to the

lists

app, Django encourages us to use a separate

lists/urls.py

, tomake

the appmore self-contained. The simplest way tomake one is to use a copy of the existing

urls.py

:

$

cp superlists/urls.py lists/

Then we replace three lines in

superlists/urls.py

with an

include

. Notice that

include

can take a part of a URL regex as a prefix, which will be applied to all the included URLs

(this is the bit where we reduce duplication, as well as giving our code a better structure):

superlists/urls.py.

urlpatterns

=

patterns

(

''

,

url

(

r'^$'

,

'lists.views.home_page'

,

name

=

'home'

),

url

(

r'^lists/'

,

include

(

'lists.urls'

)),

# url(r'^admin/', include(admin.site.urls)),

)

And

lists/urls.py

we can trim down to only include the latter part of our three URLs,

and none of the other stuff from the parent

urls.py

:

110

|

Chapter 6: Getting to the Minimum Viable Site