in apps.dashboard.tests & .urls, adding a new to-do item to the input field in either home.html or a blank-tabled list.html points to /apps/dashboard/newlist; in .views, home_page() FBV simplified to a mere html page render & new_list() FBV added that currently points to the global list
This commit is contained in:
@@ -8,23 +8,9 @@ class HomePageTest(TestCase):
|
||||
|
||||
def test_renders_input_form(self):
|
||||
response = self.client.get('/')
|
||||
self.assertContains(response, '<form method="POST" action="/">')
|
||||
self.assertContains(response, '<form method="POST" action="/apps/dashboard/newlist">')
|
||||
self.assertContains(response, '<input name="item_text"')
|
||||
|
||||
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.assertRedirects(response, '/apps/dashboard/the-only-list-in-the-world/')
|
||||
|
||||
def test_only_saves_items_when_necessary(self):
|
||||
self.client.get('/')
|
||||
self.assertEqual(Item.objects.count(), 0)
|
||||
|
||||
class ItemModelTest(TestCase):
|
||||
def test_saving_and_retrieving_items(self):
|
||||
first_item = Item()
|
||||
@@ -50,7 +36,7 @@ class DashViewTest(TestCase):
|
||||
|
||||
def test_renders_input_form(self):
|
||||
response = self.client.get('/apps/dashboard/the-only-list-in-the-world/')
|
||||
self.assertContains(response, '<form method="POST" action="/">')
|
||||
self.assertContains(response, '<form method="POST" action="/apps/dashboard/newlist">')
|
||||
self.assertContains(response, '<input name="item_text"')
|
||||
|
||||
def test_displays_all_list_items(self):
|
||||
@@ -63,3 +49,13 @@ class DashViewTest(TestCase):
|
||||
self.assertContains(response, 'itemey 1')
|
||||
self.assertContains(response, 'itemey 2')
|
||||
|
||||
class NewListTest(TestCase):
|
||||
def test_can_save_a_POST_request(self):
|
||||
self. client.post('/apps/dashboard/newlist', data={'item_text': "A new list item"})
|
||||
self.assertEqual(Item.objects.count(), 1)
|
||||
new_item = Item.objects.get()
|
||||
self.assertEqual(new_item.text, 'A new list item')
|
||||
|
||||
def test_redirects_after_POST(self):
|
||||
response = self.client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
|
||||
self.assertRedirects(response, '/apps/dashboard/the-only-list-in-the-world/')
|
||||
|
||||
@@ -2,13 +2,12 @@ from django.shortcuts import redirect, render
|
||||
from .models import Item
|
||||
|
||||
def home_page(request):
|
||||
if request.method == 'POST':
|
||||
Item.objects.create(text=request.POST['item_text'])
|
||||
return redirect('/apps/dashboard/the-only-list-in-the-world/')
|
||||
|
||||
items = Item.objects.all()
|
||||
return render(request, 'apps/dashboard/home.html', {'items': items})
|
||||
return render(request, 'apps/dashboard/home.html')
|
||||
|
||||
def view_list(request):
|
||||
items = Item.objects.all()
|
||||
return render(request, 'apps/dashboard/list.html', {'items': items})
|
||||
|
||||
def new_list(request):
|
||||
Item.objects.create(text=request.POST['item_text'])
|
||||
return redirect('/apps/dashboard/the-only-list-in-the-world/')
|
||||
|
||||
@@ -5,5 +5,6 @@ from apps.dashboard import views
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('', views.home_page, name='home'),
|
||||
path('apps/dashboard/newlist', views.new_list, name='new_list'),
|
||||
path('apps/dashboard/the-only-list-in-the-world/', views.view_list, name='view_list'),
|
||||
]
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Dashboard | Start a new to-do list</h1>
|
||||
<form method="POST" action="/">
|
||||
<form method="POST" action="/apps/dashboard/newlist">
|
||||
<input name="item_text" id="id-new-item" placeholder="Enter a to-do item">
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Your To-Do List</h1>
|
||||
<form method="POST" action="/">
|
||||
<form method="POST" action="/apps/dashboard/newlist">
|
||||
<input name="item_text" id="id-new-item" placeholder="Enter a to-do item">
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user