From 81b586ac07ea0829e0d94c2f30b3f45e634b4079 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Sun, 8 Nov 2020 16:24:07 +1100 Subject: [PATCH] 5.2 Processing a POST Request on the Server --- src/lists/tests.py | 14 +++++++------- src/lists/views.py | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lists/tests.py b/src/lists/tests.py index e4f737a..89d8566 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 django.urls import resolve from django.http import HttpRequest @@ -10,11 +11,10 @@ class HomePageTest(TestCase): found = resolve('/') self.assertEqual(found.func, home_page) - def test_home_page_returns_correct_html(self): - """ - 1. Instead of manually creating HttpRequest object and calling view function to get response, pass in URL to `self.client.get()` and get response directly - 3. `.assertTemplateUsed` is the test method that Django Test Case provides. it checks whether template was used to render a response - """ + def test_uses_home_template(self): + response = self.client.get('/') + self.assertTemplateUsed(response, 'home.html') - response = self.client.get('/') #1 - self.assertTemplateUsed(response, 'home.html') #3 \ No newline at end of file + def test_can_save_a_POST_request(self): + response = self.client.post('/', data={'item_text': 'A new list item'}) + self.assertIn('A new list item', response.content.decode()) \ No newline at end of file diff --git a/src/lists/views.py b/src/lists/views.py index f8765d7..2e0e7dd 100644 --- a/src/lists/views.py +++ b/src/lists/views.py @@ -8,4 +8,6 @@ def home_page(request): Using render function to take a request and name of the template to render """ + if request.method == 'POST': + return HttpResponse(request.POST['item_text']) return render(request, 'home.html') \ No newline at end of file