Compare commits
No commits in common. "59f8866642ca7424fe6833f5a9fb8ad5ded9441c" and "db97326010617248e42cc6fdf01bebd36aadab6c" have entirely different histories.
59f8866642
...
db97326010
|
@ -4,13 +4,13 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>Your To-Do list</h1>
|
||||
<form method="POST" action="/lists/{{ list.id }}/add_item">
|
||||
<form method="POST" action="/lists/new">
|
||||
<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 list.item_set.all %}
|
||||
{% for item in items %}
|
||||
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
from django.http import response
|
||||
from django.test import TestCase
|
||||
|
||||
from lists.models import Item, List
|
||||
|
@ -9,6 +8,10 @@ class HomePageTest(TestCase):
|
|||
response = self.client.get('/')
|
||||
self.assertTemplateUsed(response, 'home.html')
|
||||
|
||||
def test_only_saves_items_when_necessary(self):
|
||||
self.client.get('/')
|
||||
self.assertEqual(Item.objects.count(), 0)
|
||||
|
||||
class NewListTest(TestCase):
|
||||
|
||||
def test_can_save_a_POST_request(self):
|
||||
|
@ -19,65 +22,22 @@ class NewListTest(TestCase):
|
|||
|
||||
def test_redirects_after_POST(self):
|
||||
response = self.client.post('/lists/new', data={'item_text': 'A new list item'})
|
||||
new_list = List.objects.first()
|
||||
self.assertRedirects(response=response, expected_url=f'/lists/{new_list.id}/')
|
||||
|
||||
class NewItemTest(TestCase):
|
||||
|
||||
def test_can_save_a_POST_request_to_an_existing_list(self):
|
||||
other_list = List.objects.create()
|
||||
correct_list = List.objects.create()
|
||||
|
||||
self.client.post(
|
||||
f'/lists/{correct_list.id}/add_item',
|
||||
data={'item_text': 'A new item for an existing list'}
|
||||
)
|
||||
|
||||
self.assertEqual(Item.objects.count(), 1)
|
||||
new_item = Item.objects.first()
|
||||
self.assertEqual(new_item.text, 'A new item for an existing list')
|
||||
self.assertEqual(new_item.list, correct_list)
|
||||
|
||||
def test_redirects_to_list_view(self):
|
||||
other_list = List.objects.create()
|
||||
correct_list = List.objects.create()
|
||||
|
||||
response = self.client.post(
|
||||
f'/lists/{correct_list.id}/add_item',
|
||||
data={'item_text': 'A new item for an existing list'}
|
||||
)
|
||||
|
||||
self.assertRedirects(
|
||||
response=response,
|
||||
expected_url=f'/lists/{correct_list.id}/')
|
||||
self.assertRedirects(response=response, expected_url='/lists/the-only-list-in-the-world/')
|
||||
|
||||
class ListViewTest(TestCase):
|
||||
|
||||
def test_uses_list_template(self):
|
||||
list_ = List.objects.create()
|
||||
response = self.client.get(f'/lists/{list_.id}/')
|
||||
response = self.client.get('/lists/the-only-list-in-the-world/')
|
||||
self.assertTemplateUsed(response=response, template_name='list.html')
|
||||
|
||||
def test_displays_only_items_for_that_list(self):
|
||||
correct_list = List.objects.create()
|
||||
Item.objects.create(text="itemey 1", list=correct_list)
|
||||
Item.objects.create(text="itemey 2", list=correct_list)
|
||||
other_list = List.objects.create()
|
||||
Item.objects.create(text="other list item 1", list=other_list)
|
||||
Item.objects.create(text="other list item 2", list=other_list)
|
||||
|
||||
response = self.client.get(f'/lists/{correct_list.id}/')
|
||||
def test_displays_all_items(self):
|
||||
Item.objects.create(text="itemey 1")
|
||||
Item.objects.create(text="itemey 2")
|
||||
|
||||
response = self.client.get('/lists/the-only-list-in-the-world/')
|
||||
|
||||
self.assertContains(response, 'itemey 1')
|
||||
self.assertContains(response, 'itemey 2')
|
||||
self.assertNotContains(response=response, text='other list item 1')
|
||||
self.assertNotContains(response=response, text='other list item 2')
|
||||
|
||||
def test_passes_correct_list_to_template(self):
|
||||
other_list = List.objects.create()
|
||||
correct_list = List.objects.create()
|
||||
response = self.client.get(f'/lists/{correct_list.id}/')
|
||||
self.assertEqual(response.context['list'], correct_list)
|
||||
|
||||
class ListAndItemModelsTest(TestCase):
|
||||
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
"""superlists URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/1.11/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.conf.urls import url, include
|
||||
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.conf.urls import url
|
||||
from lists import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^lists/new$', views.new_list, name='new_list'),
|
||||
url(r'^lists/(\d+)/$', views.view_list, name='view_list'),
|
||||
url(r'^lists/(\d+)/add_item$', views.add_item, name='add_item'),
|
||||
]
|
|
@ -1,21 +1,17 @@
|
|||
from typing import Text
|
||||
from django.http import request
|
||||
from django.shortcuts import redirect, render
|
||||
|
||||
from lists.models import Item, List
|
||||
from lists.models import Item
|
||||
|
||||
# Create your views here
|
||||
def home_page(request):
|
||||
return render(request=request, template_name='home.html')
|
||||
|
||||
def view_list(request, list_id):
|
||||
list_ = List.objects.get(id=list_id)
|
||||
return render(request, 'list.html', {'list': list_})
|
||||
def view_list(request):
|
||||
items = Item.objects.all()
|
||||
return render(request, 'list.html', {'items': items})
|
||||
|
||||
def new_list(request):
|
||||
list_ = List.objects.create()
|
||||
Item.objects.create(text=request.POST['item_text'], list=list_)
|
||||
return redirect(f'/lists/{list_.id}/')
|
||||
|
||||
def add_item(request, list_id):
|
||||
list_ = List.objects.get(id=list_id)
|
||||
Item.objects.create(text=request.POST['item_text'], list=list_)
|
||||
return redirect(f'/lists/{list_.id}/')
|
||||
Item.objects.create(text=request.POST['item_text'])
|
||||
return redirect('/lists/the-only-list-in-the-world/')
|
|
@ -13,11 +13,11 @@ Including another URLconf
|
|||
1. Import the include() function: from django.conf.urls import url, include
|
||||
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.conf.urls import include, url
|
||||
from lists import views as list_views
|
||||
from lists import urls as list_urls
|
||||
from django.conf.urls import url
|
||||
from lists import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', list_views.home_page, name='home'),
|
||||
url(r'^lists/', include(list_urls)),
|
||||
]
|
||||
url(r'^$', views.home_page, name='home'),
|
||||
url(r'^lists/new$', views.new_list, name='new_list'),
|
||||
url(r'^lists/the-only-list-in-the-world/$', views.view_list, name='view_list'),
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue