From 8bf071ae61806df31e3509f0cf058f452198f6cf Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Wed, 11 Nov 2020 14:19:55 +1100 Subject: [PATCH] 7.7 Another Small Step: A Separate Template for Viewing Lists --- src/lists/templates/home.html | 6 ------ src/lists/templates/list.html | 18 ++++++++++++++++++ src/lists/tests.py | 4 ++++ src/lists/views.py | 7 ++----- 4 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 src/lists/templates/list.html diff --git a/src/lists/templates/home.html b/src/lists/templates/home.html index 6e154de..0709171 100644 --- a/src/lists/templates/home.html +++ b/src/lists/templates/home.html @@ -8,11 +8,5 @@ {% csrf_token %} - - - {% for item in items %} - - {% endfor %} -
{{ forloop.counter }}: {{ item.text }}
\ No newline at end of file diff --git a/src/lists/templates/list.html b/src/lists/templates/list.html new file mode 100644 index 0000000..6e154de --- /dev/null +++ b/src/lists/templates/list.html @@ -0,0 +1,18 @@ + + + To-Do lists + + +

Your To-Do list

+
+ + {% csrf_token %} +
+ + + {% for item in items %} + + {% endfor %} +
{{ forloop.counter }}: {{ item.text }}
+ + \ No newline at end of file diff --git a/src/lists/tests.py b/src/lists/tests.py index 7314889..452ad5d 100644 --- a/src/lists/tests.py +++ b/src/lists/tests.py @@ -32,6 +32,10 @@ class HomePageTest(TestCase): class ListViewTest(TestCase): + def test_uses_list_template(self): + response = self.client.get('/lists/the-only-list-in-the-world/') + self.assertTemplateUsed(response=response, template_name='list.html') + def test_displays_all_items(self): Item.objects.create(text="itemey 1") Item.objects.create(text="itemey 2") diff --git a/src/lists/views.py b/src/lists/views.py index ae78ba5..cfc8001 100644 --- a/src/lists/views.py +++ b/src/lists/views.py @@ -8,11 +8,8 @@ def home_page(request): Item.objects.create(text=request.POST['item_text']) return redirect('/lists/the-only-list-in-the-world') - items = Item.objects.all() # get objects from database (model) - return render(request=request, - template_name='home.html', - context={'items': items}) # pass items into template using render + return render(request=request, template_name='home.html') def view_list(request): items = Item.objects.all() - return render(request, 'home.html', {'items': items}) \ No newline at end of file + return render(request, 'list.html', {'items': items}) \ No newline at end of file