Compare commits

..

No commits in common. "05b8f2194354f889dd45acf76f250d0b8cb0ae86" and "8bfd695ecea5982ea4687a9859d8b12331ae4d48" have entirely different histories.

3 changed files with 16 additions and 30 deletions

View File

@ -10,9 +10,7 @@
</form>
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
<tr><td>1: {{ new_item_text }}</td></tr>
</table>
</body>
</html>

View File

@ -1,4 +1,3 @@
from typing import Text
from django.http import response
from django.test import TestCase
from django.urls import resolve
@ -18,32 +17,19 @@ 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.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
self.assertEqual(Item.objects.count(), 1) # check that one new Item has been saved to the database
new_item = Item.objects.first() # same as doing objects.all()[0]
self.assertEqual(new_item.text, 'A new list item') # check that the item's text is correct
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
def test_only_saves_items_when_necessary(self):
self.client.get('/')
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):
def test_saving_and_retrieving_items(self):

View File

@ -1,16 +1,18 @@
from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.shortcuts import redirect, render
from django.shortcuts import render
from lists.models import Item
# Create your views here
def home_page(request):
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/')
new_item_text = request.POST['item_text'] # use new_item_text to either hold POST contents or empty string
Item.objects.create(text=new_item_text) # .object.create is shortcut for creating a new Item, without needing to call .save()
else:
new_item_text = ''
return render(request=request, template_name='home.html', context={
'new_item_text': request.POST.get('item_text', ''),
})
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