Compare commits
3 Commits
8bfd695ece
...
05b8f21943
Author | SHA1 | Date |
---|---|---|
Jason Zhu | 05b8f21943 | |
Jason Zhu | ff01b880eb | |
Jason Zhu | deceab97c3 |
|
@ -10,7 +10,9 @@
|
|||
</form>
|
||||
|
||||
<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 %}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
|
@ -1,3 +1,4 @@
|
|||
from typing import Text
|
||||
from django.http import response
|
||||
from django.test import TestCase
|
||||
from django.urls import resolve
|
||||
|
@ -17,19 +18,32 @@ 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(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')
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response['location'], '/')
|
||||
|
||||
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):
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
from django.http.request import HttpRequest
|
||||
from django.http.response import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import redirect, render
|
||||
|
||||
from lists.models import Item
|
||||
|
||||
# Create your views here
|
||||
def home_page(request):
|
||||
if request.method == 'POST':
|
||||
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', ''),
|
||||
})
|
||||
Item.objects.create(text=request.POST['item_text'])
|
||||
return redirect('/')
|
||||
|
||||
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
|
Loading…
Reference in New Issue