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

lists/templates/base.html (ch18l002-2).

<ul

class=

"nav navbar-nav"

>

<li><a

href=

"{% url 'my_lists' user.email %}"

>

My lists

</a></li>

</ul>

Moving Down One Layer to View Functions (the

Controller)

That will cause a template error, so we’ll start to move down from the presentation layer

and URLs down to the controller layer, Django’s view functions.

As always, we start with a test:

lists/tests/test_views.py (ch18l003).

class

MyListsTest

(

TestCase

):

def

test_my_lists_url_renders_my_lists_template

(

self

):

response

=

self

.

client

.

get

(

'/lists/users

/a@b.com/

'

)

self

.

assertTemplateUsed

(

response

,

'my_lists.html'

)

That gives:

AssertionError: No templates used to render the response

And we fix it, still at the presentation level, in

urls.py

:

lists/urls.py.

urlpatterns

=

patterns

(

''

,

url

(

r'^(\d+)/$'

,

'lists.views.view_list'

,

name

=

'view_list'

),

url

(

r'^new$'

,

'lists.views.new_list'

,

name

=

'new_list'

),

url

(

r'^users/(.+)/$'

,

'lists.views.my_lists'

,

name

=

'my_lists'

),

)

That gives us a test failure, which informs us of what we should do as we move down

to the next level:

django.core.exceptions.ViewDoesNotExist: Could not import lists.views.my_lists.

View does not exist in module lists.views.

We move in from the presentation layer to the views layer, and create a minimal

placeholder:

lists/views.py (ch18l005).

def

my_lists

(

request

,

email

):

return

render

(

request

,

'my_lists.html'

)

And, a minimal template:

lists/templates/my_lists.html.

{% extends 'base.html' %}

{% block header_text %}My Lists{% endblock %}

That gets our unit tests passing, but our FT is still at the same point, saying that the “My

Lists” page doesn’t yet show any lists. It wants them to be clickable links named after the

first item:

326

|

Chapter 18: Finishing “My Lists”: Outside-In TDD