diff --git a/src/lists/templates/home.html b/src/lists/templates/home.html index a055bb0..4b18e1d 100644 --- a/src/lists/templates/home.html +++ b/src/lists/templates/home.html @@ -10,7 +10,9 @@ - + {% for item in items %} + + {% endfor %}
1: {{ new_item_text }}
1: {{ item.text }}
\ No newline at end of file diff --git a/src/lists/tests.py b/src/lists/tests.py index 44e7101..4102f7f 100644 --- a/src/lists/tests.py +++ b/src/lists/tests.py @@ -1,3 +1,4 @@ +from typing import Text from django.http import response from django.test import TestCase from django.urls import resolve @@ -17,12 +18,15 @@ class HomePageTest(TestCase): self.assertTemplateUsed(response, 'home.html') def test_can_save_a_POST_request(self): - response = self.client.post('/', data={'item_text': 'A new list item'}) + self.client.post('/', data={'item_text': 'A new list item'}) self.assertEqual(Item.objects.count(), 1) new_item = Item.objects.first() self.assertEqual(new_item.text, 'A new list item') + def test_redirects_after_POST(self): + response = self.client.post('/', data={'item_text': 'A new list item'}) + self.assertEqual(response.status_code, 302) self.assertEqual(response['location'], '/') @@ -30,6 +34,16 @@ class HomePageTest(TestCase): self.client.get('/') self.assertEqual(Item.objects.count(), 0) + def test_displays_all_list_items(self): + Item.objects.create(text="itemey 1") + Item.objects.create(text="itemey 2") + + response = self.client.get('/') + + self.assertIn('itemey 1', response.content.decode()) + self.assertIn('itemey 2', response.content.decode()) + + class ItemModelTest(TestCase): def test_saving_and_retrieving_items(self): diff --git a/src/lists/views.py b/src/lists/views.py index d5e89eb..c21ec5f 100644 --- a/src/lists/views.py +++ b/src/lists/views.py @@ -10,4 +10,7 @@ def home_page(request): Item.objects.create(text=request.POST['item_text']) return redirect('/') - return render(request=request, template_name='home.html') \ No newline at end of file + 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 \ No newline at end of file