7.11.4 Testing the Response Context Objects Directly

chap7-new
Jason Zhu 2020-11-11 15:47:02 +11:00
parent d99dd167ac
commit e3f0793729
3 changed files with 10 additions and 5 deletions

View File

@ -4,13 +4,13 @@
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST" action="/lists/new">
<form method="POST" action="/lists/{{ list.id }}/add_item">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<table id="id_list_table">
{% for item in items %}
{% for item in list.item_set.all %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>

View File

@ -1,3 +1,4 @@
from django.http import response
from django.test import TestCase
from lists.models import Item, List
@ -65,7 +66,6 @@ class ListViewTest(TestCase):
Item.objects.create(text="other list item 1", list=other_list)
Item.objects.create(text="other list item 2", list=other_list)
response = self.client.get(f'/lists/{correct_list.id}/')
self.assertContains(response, 'itemey 1')
@ -73,6 +73,12 @@ class ListViewTest(TestCase):
self.assertNotContains(response=response, text='other list item 1')
self.assertNotContains(response=response, text='other list item 2')
def test_passes_correct_list_to_template(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.get(f'/lists/{correct_list.id}/')
self.assertEqual(response.context['list'], correct_list)
class ListAndItemModelsTest(TestCase):
def test_saving_and_retrieving_items(self):

View File

@ -8,8 +8,7 @@ def home_page(request):
def view_list(request, list_id):
list_ = List.objects.get(id=list_id)
items = Item.objects.filter(list=list_)
return render(request, 'list.html', {'items': items})
return render(request, 'list.html', {'list': list_})
def new_list(request):
list_ = List.objects.create()