From e3f0793729e6061a48d3ec8194c8cf4075e98fc6 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Wed, 11 Nov 2020 15:47:02 +1100 Subject: [PATCH] 7.11.4 Testing the Response Context Objects Directly --- src/lists/templates/list.html | 4 ++-- src/lists/tests.py | 8 +++++++- src/lists/views.py | 3 +-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lists/templates/list.html b/src/lists/templates/list.html index ca484f0..f27d3bb 100644 --- a/src/lists/templates/list.html +++ b/src/lists/templates/list.html @@ -4,13 +4,13 @@

Your To-Do list

-
+ {% csrf_token %}
- {% for item in items %} + {% for item in list.item_set.all %} {% endfor %}
{{ forloop.counter }}: {{ item.text }}
diff --git a/src/lists/tests.py b/src/lists/tests.py index 56bfe48..003d618 100644 --- a/src/lists/tests.py +++ b/src/lists/tests.py @@ -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): diff --git a/src/lists/views.py b/src/lists/views.py index fe727ba..f357537 100644 --- a/src/lists/views.py +++ b/src/lists/views.py @@ -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()