5.3 Passing Python Variables to Be Rendered in the Template

This commit is contained in:
Jason Zhu 2020-11-08 16:50:20 +11:00
parent 81b586ac07
commit c0661056cf
3 changed files with 10 additions and 9 deletions

View File

@ -9,6 +9,8 @@
{% csrf_token %}
</form>
<table id="id_list_table"></table>
<table id="id_list_table">
<tr><td>{{ new_item_text }}</td></tr>
</table>
</body>
</html>

View File

@ -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())
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')

View File

@ -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')
return render(request=request, template_name='home.html', context={
'new_item_text': request.POST['item_text'],
})