7.8.2 A URL and View for New List Creation

chap7-new
Jason Zhu 2020-11-11 14:32:21 +11:00
parent 24fb7b1e09
commit b5ab7f620a
2 changed files with 8 additions and 1 deletions

View File

@ -1,3 +1,5 @@
from typing import Text
from django.http import request
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from lists.models import Item from lists.models import Item
@ -12,4 +14,8 @@ def home_page(request):
def view_list(request): def view_list(request):
items = Item.objects.all() items = Item.objects.all()
return render(request, 'list.html', {'items': items}) return render(request, 'list.html', {'items': items})
def new_list(request):
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world/')

View File

@ -18,5 +18,6 @@ from lists import views
urlpatterns = [ urlpatterns = [
url(r'^$', views.home_page, name='home'), url(r'^$', views.home_page, name='home'),
url(r'^lists/new$', views.new_list, name='new_list'),
url(r'^lists/the-only-list-in-the-world/$', views.view_list, name='view_list'), url(r'^lists/the-only-list-in-the-world/$', views.view_list, name='view_list'),
] ]