Rendering Items in the Template
This commit is contained in:
parent
deceab97c3
commit
ff01b880eb
@ -10,7 +10,9 @@
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
<table id="id_list_table">
|
<table id="id_list_table">
|
||||||
<tr><td>1: {{ new_item_text }}</td></tr>
|
{% for item in items %}
|
||||||
|
<tr><td>1: {{ item.text }}</td></tr>
|
||||||
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,3 +1,4 @@
|
|||||||
|
from typing import Text
|
||||||
from django.http import response
|
from django.http import response
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.urls import resolve
|
from django.urls import resolve
|
||||||
@ -17,12 +18,15 @@ class HomePageTest(TestCase):
|
|||||||
self.assertTemplateUsed(response, 'home.html')
|
self.assertTemplateUsed(response, 'home.html')
|
||||||
|
|
||||||
def test_can_save_a_POST_request(self):
|
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)
|
self.assertEqual(Item.objects.count(), 1)
|
||||||
new_item = Item.objects.first()
|
new_item = Item.objects.first()
|
||||||
self.assertEqual(new_item.text, 'A new list item')
|
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.status_code, 302)
|
||||||
self.assertEqual(response['location'], '/')
|
self.assertEqual(response['location'], '/')
|
||||||
|
|
||||||
@ -30,6 +34,16 @@ class HomePageTest(TestCase):
|
|||||||
self.client.get('/')
|
self.client.get('/')
|
||||||
self.assertEqual(Item.objects.count(), 0)
|
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):
|
class ItemModelTest(TestCase):
|
||||||
|
|
||||||
def test_saving_and_retrieving_items(self):
|
def test_saving_and_retrieving_items(self):
|
||||||
|
@ -10,4 +10,7 @@ def home_page(request):
|
|||||||
Item.objects.create(text=request.POST['item_text'])
|
Item.objects.create(text=request.POST['item_text'])
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
return render(request=request, template_name='home.html')
|
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
|
Loading…
x
Reference in New Issue
Block a user