From c0661056cf61290ba38d590e27138153c53a0040 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Sun, 8 Nov 2020 16:50:20 +1100 Subject: [PATCH] 5.3 Passing Python Variables to Be Rendered in the Template --- src/lists/templates/home.html | 4 +++- src/lists/tests.py | 5 ++++- src/lists/views.py | 10 +++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lists/templates/home.html b/src/lists/templates/home.html index 87e5ae9..34c2117 100644 --- a/src/lists/templates/home.html +++ b/src/lists/templates/home.html @@ -9,6 +9,8 @@ {% csrf_token %} -
+ + +
{{ new_item_text }}
\ No newline at end of file diff --git a/src/lists/tests.py b/src/lists/tests.py index 89d8566..31ff19b 100644 --- a/src/lists/tests.py +++ b/src/lists/tests.py @@ -17,4 +17,7 @@ class HomePageTest(TestCase): 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 + self.assertIn('A new list item', response.content.decode()) + self.assertTemplateUsed(response, 'home.html') + + \ No newline at end of file diff --git a/src/lists/views.py b/src/lists/views.py index 2e0e7dd..8927dc9 100644 --- a/src/lists/views.py +++ b/src/lists/views.py @@ -4,10 +4,6 @@ from django.shortcuts import render # Create your views here 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 + return render(request=request, template_name='home.html', context={ + 'new_item_text': request.POST['item_text'], + }) \ No newline at end of file