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

(lists.tests.test_views.ListViewTest)

AssertionError: 0 != 1

We change the

view_list

function to handle two types of request:

lists/views.py (ch10l022-1).

def

view_list

(

request

,

list_id

):

list_

=

List

.

objects

.

get

(

id

=

list_id

)

if

request

.

method

==

'POST'

:

Item

.

objects

.

create

(

text

=

request

.

POST

[

'item_text'

],

list

=

list_

)

return

redirect

(

'/lists/

%d

/'

%

(

list_

.

id

,))

return

render

(

request

,

'list.html'

, {

'list'

:

list_

})

That gets us passing tests:

Ran 13 tests in 0.047s

OK

Now we can delete the

add_item

view, since it’s no longer needed … oops, a couple of

unexpected failures:

[...]

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

View does not exist in module lists.views.

[...]

FAILED (errors=4)

It’s because we’ve deleted the view, but it’s still being referred to in

urls.py

. We remove

it from there:

lists/urls.py (ch10l023).

urlpatterns

=

patterns

(

''

,

url

(

r'^(\d+)/$'

,

'lists.views.view_list'

,

name

=

'view_list'

),

url

(

r'^new$'

,

'lists.views.new_list'

,

name

=

'new_list'

),

)

And that gets us to the

OK

. Let’s try a full FT run:

$

python3 manage.py test functional_tests

[...]

Ran 3 tests in 15.276s

FAILED (errors=1)

We’re back to the one failure in our new functional test. Our refactor of the

add_item

functionality is complete. We should commit there:

$

git commit -am"Refactor list view to handle new item POSTs"

Django Pattern: Processing POST Requests in the Same View as Renders the Form

|

185