From db72024d5464859f565aa0882fb34eac1b48286b Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:18:34 +1000 Subject: [PATCH] Content for session 9. Tidy up URLs and Views. --- .../community_db/person_detail_in_base.html | 14 +++++++ .../community_db/person_list_in_base.html | 7 +++- src/community_db/views.py | 40 +++++++------------ src/pacificconnect/urls.py | 15 +++++-- 4 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 src/community_db/templates/community_db/person_detail_in_base.html diff --git a/src/community_db/templates/community_db/person_detail_in_base.html b/src/community_db/templates/community_db/person_detail_in_base.html new file mode 100644 index 0000000..31102ff --- /dev/null +++ b/src/community_db/templates/community_db/person_detail_in_base.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block content %} +
+ Back to list +
+ This is the detail of a person: + +{% endblock %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html index bca85c5..d9d4ede 100644 --- a/src/community_db/templates/community_db/person_list_in_base.html +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -4,8 +4,11 @@ This is my list of folks {% endblock %} \ No newline at end of file diff --git a/src/community_db/views.py b/src/community_db/views.py index 1b0d3dd..d6ea8e0 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,41 +1,29 @@ -from django.http import HttpResponse from django.shortcuts import render -from django.template import loader -from django.views.generic.list import ListView +from django.views.generic import DetailView, ListView -from community_db.models import Person +from .models import Person - -def list_persons(request): - html = "This is my list of folks" - return HttpResponse(html) - - -# def list_persons_with_template(request): -# persons = Person.objects.all() -# template = loader.get_template("community_db/person_list.html") -# context = {"object_list": persons} -# return HttpResponse(template.render(context, request)) +# FUNCTION BASED VIEWS def list_persons_with_template(request): persons = Person.objects.all() - template = loader.get_template("community_db/person_list_in_base.html") context = {"object_list": persons} - return HttpResponse(template.render(context, request)) + return render(request, "community_db/person_list_in_base.html", context) -# def list_persons_with_template(request): -# persons = Person.objects.all() -# context = {"object_list": persons} -# return render(request, "community_db/person_list.html", context) +def detail_person_with_template(request, pk): + person = Person.objects.get(id=pk) + context = {"object": person} + return render(request, "community_db/person_detail_in_base.html", context) +# CLASS BASED VIEWS class PersonListView(ListView): model = Person template_name = "community_db/person_list_in_base.html" + + +class PersonDetailView(DetailView): + model = Person + template_name = "community_db/person_detail_in_base.html" diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index c06409c..d602477 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -20,7 +20,16 @@ urlpatterns = [ path("admin/", admin.site.urls), - path("myview", views.list_persons), - path("myview-with-template/", views.list_persons_with_template), - path("person-list/", views.PersonListView.as_view()), + path("fbv/people/", views.list_persons_with_template, name="fbv-person-list"), + path( + "fbv/people//", + views.detail_person_with_template, + name="fbv-person-detail", + ), + path("cbv/people/", views.PersonListView.as_view(), name="cbv-person-list"), + path( + "cbv/people//", + views.PersonDetailView.as_view(), + name="cbv-person-detail", + ), ]