7.7 Another Small Step: A Separate Template for Viewing Lists

This commit is contained in:
Jason Zhu 2020-11-11 14:19:55 +11:00
parent 541e85a01f
commit 8bf071ae61
4 changed files with 24 additions and 11 deletions

View File

@ -8,11 +8,5 @@
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
</body>
</html>

View File

@ -0,0 +1,18 @@
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST" action="/">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
</body>
</html>

View File

@ -32,6 +32,10 @@ class HomePageTest(TestCase):
class ListViewTest(TestCase):
def test_uses_list_template(self):
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertTemplateUsed(response=response, template_name='list.html')
def test_displays_all_items(self):
Item.objects.create(text="itemey 1")
Item.objects.create(text="itemey 2")

View File

@ -8,11 +8,8 @@ def home_page(request):
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world')
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
return render(request=request, template_name='home.html')
def view_list(request):
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
return render(request, 'list.html', {'items': items})