![Show Menu](styles/mobile-menu.png)
![Page Background](./../common/page-substrates/page0133.jpg)
The functional tests have warned us of a regression in our application: because we’re
now creating a new list for every single POST submission, we have broken the ability
to add multiple items to a list. This is exactly what we have functional tests for!
And it correlates nicely with the last item on our to-do list:
•
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
One More View to Handle Adding Items to an Existing List
We need a URL and view to handle adding a new item to an existing list (
/lists/<list_id>/
add_item
). We’re getting pretty good at these now, so let’s knock one together quickly:
lists/tests.py.
class
NewItemTest
(
TestCase
):
def
test_can_save_a_POST_request_to_an_existing_list
(
self
):
other_list
=
List
.
objects
.
create
()
correct_list
=
List
.
objects
.
create
()
self
.
client
.
post
(
'/lists/
%d
/add_item'
%
(
correct_list
.
id
,),
data
=
{
'item_text'
:
'A new item for an existing list'
}
)
self
.
assertEqual
(
Item
.
objects
.
count
(),
1
)
new_item
=
Item
.
objects
.
first
()
self
.
assertEqual
(
new_item
.
text
,
'A new item for an existing list'
)
self
.
assertEqual
(
new_item
.
list
,
correct_list
)
def
test_redirects_to_list_view
(
self
):
other_list
=
List
.
objects
.
create
()
correct_list
=
List
.
objects
.
create
()
One More View to Handle Adding Items to an Existing List
|
105