diff --git a/lists/__pycache__/models.cpython-38.pyc b/lists/__pycache__/models.cpython-38.pyc index a36af7d79607bf711539b1f63d05c14c21d65427..c6edec9e458fb35da70716cf2cd0e239a189afce 100644 Binary files a/lists/__pycache__/models.cpython-38.pyc and b/lists/__pycache__/models.cpython-38.pyc differ diff --git a/lists/__pycache__/tests.cpython-38.pyc b/lists/__pycache__/tests.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d67a369ec4ecd427b1185f7879b5398087f57541 Binary files /dev/null and b/lists/__pycache__/tests.cpython-38.pyc differ diff --git a/lists/__pycache__/views.cpython-38.pyc b/lists/__pycache__/views.cpython-38.pyc index f33d14f48356ee6888215a7e01e0da0b90af4ae5..b16b805213660d482cf16968de9505115f0567c5 100644 Binary files a/lists/__pycache__/views.cpython-38.pyc and b/lists/__pycache__/views.cpython-38.pyc differ diff --git a/lists/migrations/0001_initial.py b/lists/migrations/0001_initial.py new file mode 100644 index 0000000000000000000000000000000000000000..e1f87d1e6bf4a66268887523749edb05c7010496 --- /dev/null +++ b/lists/migrations/0001_initial.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.5 on 2019-09-26 15:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Item', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + ), + ] diff --git a/lists/migrations/0002_item_text.py b/lists/migrations/0002_item_text.py new file mode 100644 index 0000000000000000000000000000000000000000..70b573fed753a37f81841a60cd8b3628bbb06cd7 --- /dev/null +++ b/lists/migrations/0002_item_text.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.5 on 2019-09-26 15:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lists', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='item', + name='text', + field=models.TextField(default=''), + ), + ] diff --git a/lists/migrations/__pycache__/0001_initial.cpython-38.pyc b/lists/migrations/__pycache__/0001_initial.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ee1e90758f19fbaf9bae9717e4f57d55e1603659 Binary files /dev/null and b/lists/migrations/__pycache__/0001_initial.cpython-38.pyc differ diff --git a/lists/migrations/__pycache__/0002_item_text.cpython-38.pyc b/lists/migrations/__pycache__/0002_item_text.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..152ed7efbf0c976468572716f6feb320f331742d Binary files /dev/null and b/lists/migrations/__pycache__/0002_item_text.cpython-38.pyc differ diff --git a/lists/models.py b/lists/models.py index 71a836239075aa6e6e4ecb700e9c42c95c022d91..02be5df811fa8de7051030077d725a8ef9e02d5b 100644 --- a/lists/models.py +++ b/lists/models.py @@ -1,3 +1,4 @@ from django.db import models -# Create your models here. +class Item(models.Model): + text = models.TextField(default='') \ No newline at end of file diff --git a/lists/templates/home.html b/lists/templates/home.html index 3bedb83ea7af2a78b46d3d96af990e14caf59eba..9ad5489d7e4c6cf1200aca02faa96fe6787084a4 100644 --- a/lists/templates/home.html +++ b/lists/templates/home.html @@ -1,5 +1,18 @@ To-Do lists -

Alvin Raihan

-

To-Do

+ +

Alvin Raihan

+

To-Do

+ +

Your To-Do list

+
+ + {% csrf_token %} +
+ + {% for item in items %} + + {% endfor %} +
{{ forloop.counter }}: {{ item.text }}
+ \ No newline at end of file diff --git a/lists/tests.py b/lists/tests.py index 75346f61505161733e3778a0269702cef84f8f0e..9fd32d32d51337835c583309d7e81963160692b7 100644 --- a/lists/tests.py +++ b/lists/tests.py @@ -3,6 +3,7 @@ from django.test import TestCase from django.http import HttpRequest from lists.views import home_page +from lists.models import Item class HomePageTest(TestCase): @@ -10,12 +11,52 @@ class HomePageTest(TestCase): def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertEqual(found.func, home_page) - - def test_home_page_returns_correct_html(self): request = HttpRequest() response = home_page(request) html = response.content.decode('utf8') self.assertTrue(html.startswith('')) self.assertIn('To-Do lists', html) - self.assertTrue(html.endswith('')) \ No newline at end of file + self.assertTrue(html.endswith('')) + def test_uses_home_template(self): + response = self.client.get('/') + self.assertTemplateUsed(response, 'home.html') + def test_can_save_a_POST_request(self): + response = 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') + 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) + +class ItemModelTest(TestCase): + + def test_saving_and_retrieving_items(self): + first_item = Item() + first_item.text = 'The first (ever) list item' + first_item.save() + + second_item = Item() + second_item.text = 'Item the second' + second_item.save() + + saved_items = Item.objects.all() + self.assertEqual(saved_items.count(), 2) + + first_saved_item = saved_items[0] + second_saved_item = saved_items[1] + self.assertEqual(first_saved_item.text, 'The first (ever) list item') + self.assertEqual(second_saved_item.text, 'Item the second') + + def test_can_save_a_POST_request(self): + response = 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') + + self.assertIn('A new list item', response.content.decode()) + self.assertTemplateUsed(response, 'home.html') \ No newline at end of file diff --git a/lists/views.py b/lists/views.py index e90445a5b03f975c07a64376b3bd5a0fb41e1c51..36b4b4a754339487436ebc0d84fc695d2bee209d 100644 --- a/lists/views.py +++ b/lists/views.py @@ -1,4 +1,11 @@ -from django.shortcuts import render +from django.http import HttpResponse +from django.shortcuts import redirect,render +from lists.models import Item def home_page(request): - return render(request, 'home.html') \ No newline at end of file + if request.method == 'POST': + Item.objects.create(text=request.POST['item_text']) + return redirect('/') + + items = Item.objects.all() + return render(request, 'home.html', {'items': items}) \ No newline at end of file