Compare commits
3 Commits
8bfd695ece
...
05b8f21943
Author | SHA1 | Date |
---|---|---|
Jason Zhu | 05b8f21943 | |
Jason Zhu | ff01b880eb | |
Jason Zhu | deceab97c3 |
|
@ -10,7 +10,9 @@
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<table id="id_list_table">
|
<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>
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -1,3 +1,4 @@
|
||||||
|
from typing import Text
|
||||||
from django.http import response
|
from django.http import response
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.urls import resolve
|
from django.urls import resolve
|
||||||
|
@ -17,19 +18,32 @@ class HomePageTest(TestCase):
|
||||||
self.assertTemplateUsed(response, 'home.html')
|
self.assertTemplateUsed(response, 'home.html')
|
||||||
|
|
||||||
def test_can_save_a_POST_request(self):
|
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'})
|
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
|
self.assertEqual(response.status_code, 302)
|
||||||
new_item = Item.objects.first() # same as doing objects.all()[0]
|
self.assertEqual(response['location'], '/')
|
||||||
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):
|
def test_only_saves_items_when_necessary(self):
|
||||||
self.client.get('/')
|
self.client.get('/')
|
||||||
self.assertEqual(Item.objects.count(), 0)
|
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):
|
class ItemModelTest(TestCase):
|
||||||
|
|
||||||
def test_saving_and_retrieving_items(self):
|
def test_saving_and_retrieving_items(self):
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
from django.http.request import HttpRequest
|
from django.http.request import HttpRequest
|
||||||
from django.http.response import HttpResponse
|
from django.http.response import HttpResponse
|
||||||
from django.shortcuts import render
|
from django.shortcuts import redirect, render
|
||||||
|
|
||||||
from lists.models import Item
|
from lists.models import Item
|
||||||
|
|
||||||
# Create your views here
|
# Create your views here
|
||||||
def home_page(request):
|
def home_page(request):
|
||||||
if request.method == 'POST':
|
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=request.POST['item_text'])
|
||||||
Item.objects.create(text=new_item_text) # .object.create is shortcut for creating a new Item, without needing to call .save()
|
return redirect('/')
|
||||||
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
|
Loading…
Reference in New Issue