Fakultas Ilmu Komputer UI

Commit 6acea56d authored by emil farisan's avatar emil farisan
Browse files

Redirect after POST, and show all items in template

parent dc5d3293
......@@ -10,7 +10,9 @@
<table id="id_list_table">
<tr><td>1: {{ new_item_text }}</td></tr>
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
\ No newline at end of file
......@@ -8,9 +8,16 @@ class HomePageTest(TestCase):
self.assertTemplateUsed(response, 'home.html')
def test_can_save_a_POST_request(self):
self.client.post('/', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
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.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
def test_home_page_returns_correct_html(self):
response = self.client.get('/')
......@@ -20,6 +27,19 @@ class HomePageTest(TestCase):
self.assertTemplateUsed(response, 'home.html')
def test_only_saves_items_when_necessary(self):
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):
from django.shortcuts import render
from django.shortcuts import redirect, render
from lists.models import Item
def home_page(request):
return render(request, 'home.html', {
'new_item_text': request.POST.get('item_text', ''),
if request.method == 'POST':
return redirect('/')
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment