Browse Source

refactored urls.py

david 5 years ago
parent
commit
22120df16f

+ 1 - 1
SCRATCHPAD.txt

@@ -9,6 +9,6 @@
 - Add unique URLs for each list
 - Add a URL for creating a new list via POST
 - Add URLs for adding a new item to an existing list via POST
-+ Refactor away some duplication in urls.py
+- Refactor away some duplication in urls.py
 
 + Make a URL to view all lists made (Incase you loose the link that link can be found again)

+ 21 - 0
lists/migrations/0003_list.py

@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.21 on 2019-06-20 03:55
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('lists', '0002_item_text'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='List',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+            ],
+        ),
+    ]

+ 20 - 0
lists/migrations/0004_item_listi.py

@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.21 on 2019-06-20 16:34
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('lists', '0003_list'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='item',
+            name='listi',
+            field=models.TextField(default=''),
+        ),
+    ]

+ 21 - 0
lists/migrations/0005_auto_20190620_1635.py

@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.21 on 2019-06-20 16:35
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('lists', '0004_item_listi'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='item',
+            name='listi',
+            field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='lists.List'),
+        ),
+    ]

+ 17 - 0
lists/templates/list.html

@@ -0,0 +1,17 @@
+<html>
+    <head>
+        <title>To-Do lists</title>
+    </head>
+    <body>
+        <h1>Your To-Do list</h1>
+        <form method="POST" action="/lists/{{ list.id }}/add_item">
+            <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 %}
+                <tr><td>{{forloop.counter}}: {{ item.text }}</td></tr>
+            {% endfor %}
+        </table>
+    </body>
+</html>

+ 23 - 0
lists/urls.py

@@ -0,0 +1,23 @@
+"""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'^new$', views.new_list, name='new_list'),
+    url(r'^(\d+)/$', views.view_list,  name='view_list'),
+    url(r'^(\d+)/add_item$', views.add_item, name='add_item'),
+]

+ 5 - 6
superlists/urls.py

@@ -13,12 +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 url
-from lists import views
+from django.conf.urls import url, include
+from lists import views as list_views
+from lists import urls as list_urls
 
 urlpatterns = [
-    url(r'^$', views.home_page, name='home'),
-    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'),
+    url(r'^$', list_views.home_page, name='home'),
+    url(r'^lists/', include(list_urls)),
 ]