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:
Disco DeDisco
2026-01-02 21:50:37 -05:00
parent 87e40d977c
commit b4b2b43475
5 changed files with 22 additions and 26 deletions

View File

@@ -8,23 +8,9 @@ class HomePageTest(TestCase):
def test_renders_input_form(self): def test_renders_input_form(self):
response = self.client.get('/') 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"') 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): class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self): def test_saving_and_retrieving_items(self):
first_item = Item() first_item = Item()
@@ -50,7 +36,7 @@ class DashViewTest(TestCase):
def test_renders_input_form(self): def test_renders_input_form(self):
response = self.client.get('/apps/dashboard/the-only-list-in-the-world/') 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"') self.assertContains(response, '<input name="item_text"')
def test_displays_all_list_items(self): def test_displays_all_list_items(self):
@@ -62,4 +48,14 @@ class DashViewTest(TestCase):
# Then/Assert # Then/Assert
self.assertContains(response, 'itemey 1') self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2') 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/')

View File

@@ -2,13 +2,12 @@ from django.shortcuts import redirect, render
from .models import Item from .models import Item
def home_page(request): def home_page(request):
if request.method == 'POST': return render(request, 'apps/dashboard/home.html')
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})
def view_list(request): def view_list(request):
items = Item.objects.all() items = Item.objects.all()
return render(request, 'apps/dashboard/list.html', {'items': items}) 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/')

View File

@@ -5,5 +5,6 @@ from apps.dashboard import views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', views.home_page, name='home'), 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'), path('apps/dashboard/the-only-list-in-the-world/', views.view_list, name='view_list'),
] ]

View File

@@ -4,7 +4,7 @@
</head> </head>
<body> <body>
<h1>Dashboard | Start a new to-do list</h1> <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"> <input name="item_text" id="id-new-item" placeholder="Enter a to-do item">
{% csrf_token %} {% csrf_token %}
</form> </form>

View File

@@ -4,7 +4,7 @@
</head> </head>
<body> <body>
<h1>Your To-Do List</h1> <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"> <input name="item_text" id="id-new-item" placeholder="Enter a to-do item">
{% csrf_token %} {% csrf_token %}
</form> </form>