From 13f302ea9b63b0e02a7ab86ff3eedbebbd0004b2 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 11 Aug 2022 15:43:02 +1000 Subject: [PATCH 1/9] Update for session 8 content --- src/community_db/admin.py | 12 +++++- src/community_db/models.py | 2 +- src/community_db/templates/base.html | 9 +++++ .../templates/community_db/person_list.html | 13 ++++++ .../community_db/person_list_in_base.html | 11 +++++ src/community_db/views.py | 40 ++++++++++++++++++- src/pacificconnect/urls.py | 5 +++ 7 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/community_db/templates/base.html create mode 100644 src/community_db/templates/community_db/person_list.html create mode 100644 src/community_db/templates/community_db/person_list_in_base.html diff --git a/src/community_db/admin.py b/src/community_db/admin.py index 8c38f3f..e7a3b1f 100644 --- a/src/community_db/admin.py +++ b/src/community_db/admin.py @@ -1,3 +1,13 @@ from django.contrib import admin -# Register your models here. +from .models import Person + + +class PersonAdmin(admin.ModelAdmin): + list_display = ( + "first_name", + "last_name", + ) + + +admin.site.register(Person, PersonAdmin) diff --git a/src/community_db/models.py b/src/community_db/models.py index f0853ab..9106665 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -3,6 +3,6 @@ class Person(models.Model): first_name = models.CharField(max_length=100) - last_name = models.CharField(max_length=100, blank=True) + last_name = models.CharField(max_length=100) country = models.CharField(max_length=100, blank=True) mobile_number = models.CharField(max_length=20, blank=True) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html new file mode 100644 index 0000000..56ac5bc --- /dev/null +++ b/src/community_db/templates/base.html @@ -0,0 +1,9 @@ + + + +

Welcome to the Pacific Connect Community Database

+ On this site, you can find details of members of the Pacific Connect Community Database + {% block content %}{% endblock %} + + + \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_list.html b/src/community_db/templates/community_db/person_list.html new file mode 100644 index 0000000..92c0a9a --- /dev/null +++ b/src/community_db/templates/community_db/person_list.html @@ -0,0 +1,13 @@ + + + + This is my list of folks + + + + \ 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 new file mode 100644 index 0000000..bca85c5 --- /dev/null +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} + 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 91ea44a..1b0d3dd 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,41 @@ +from django.http import HttpResponse from django.shortcuts import render +from django.template import loader +from django.views.generic.list import ListView -# Create your views here. +from community_db.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)) + + +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)) + + +# def list_persons_with_template(request): +# persons = Person.objects.all() +# context = {"object_list": persons} +# return render(request, "community_db/person_list.html", context) + + +class PersonListView(ListView): + model = Person + template_name = "community_db/person_list_in_base.html" diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index 5a454ad..c06409c 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -16,6 +16,11 @@ from django.contrib import admin from django.urls import path +from community_db import views + 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()), ] From db72024d5464859f565aa0882fb34eac1b48286b Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:18:34 +1000 Subject: [PATCH 2/9] 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", + ), ] From 813a8090a613aaeb947bc75c7565a0c296ace88a Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:54:17 +1000 Subject: [PATCH 3/9] Session 10 - add search --- src/community_db/templates/base.html | 4 ++++ src/community_db/views.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index 56ac5bc..0aeafd8 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -3,6 +3,10 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database +
+
+ Search: +
{% block content %}{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index d6ea8e0..4a318ec 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,4 @@ +from django.db import models from django.shortcuts import render from django.views.generic import DetailView, ListView @@ -6,8 +7,13 @@ # FUNCTION BASED VIEWS +# Searching only the first_name field def list_persons_with_template(request): + search_text = request.GET.get("search") + persons = Person.objects.all() + if search_text: + persons = persons.filter(first_name__icontains=search_text) context = {"object_list": persons} return render(request, "community_db/person_list_in_base.html", context) From 509bb13b9c1b051eb98a425f1ed56f0727f5f101 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:55:46 +1000 Subject: [PATCH 4/9] Session 10 - multi-field search --- src/community_db/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/community_db/views.py b/src/community_db/views.py index 4a318ec..908cd6d 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -6,14 +6,16 @@ # FUNCTION BASED VIEWS - -# Searching only the first_name field +# Searching the first name and last name fields def list_persons_with_template(request): search_text = request.GET.get("search") persons = Person.objects.all() if search_text: - persons = persons.filter(first_name__icontains=search_text) + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + persons = persons.filter(search_filters) context = {"object_list": persons} return render(request, "community_db/person_list_in_base.html", context) From 80a5ffbfc1be655dfa258d57e6bc94ed43feef46 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 12:05:28 +1000 Subject: [PATCH 5/9] Session 10 - Add search to generic view --- src/community_db/views.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/community_db/views.py b/src/community_db/views.py index 908cd6d..fa34fac 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -31,6 +31,25 @@ class PersonListView(ListView): model = Person template_name = "community_db/person_list_in_base.html" + def get_queryset(self): + # Get the default queryset - i.e. set of rows - that we want + # to filter + queryset = super().get_queryset() + + # Get the search field value - but from self.request in the + # class based view + search_text = self.request.GET.get("search") + + # Filter if we need to + if search_text: + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + queryset = queryset.filter(search_filters) + + # Return the queryset now that we have filtered it (if we need to) + return queryset + class PersonDetailView(DetailView): model = Person From 91af5567d76edf8f572bbe285a0b2193b0da4f87 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:58:21 +1000 Subject: [PATCH 6/9] Session 10 - text in search box --- src/community_db/templates/base.html | 2 +- src/community_db/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index 0aeafd8..f2ed587 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,7 +5,7 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- Search: + Search:
{% block content %}{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index fa34fac..79d176f 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -6,7 +6,7 @@ # FUNCTION BASED VIEWS -# Searching the first name and last name fields +# Searching the first name and last name fields with text in the search box def list_persons_with_template(request): search_text = request.GET.get("search") @@ -16,7 +16,7 @@ def list_persons_with_template(request): last_name__icontains=search_text ) persons = persons.filter(search_filters) - context = {"object_list": persons} + context = {"object_list": persons, "search_text": search_text} return render(request, "community_db/person_list_in_base.html", context) From a3cb85e9be4a8767f64a033681d839bbe9b8a574 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 14:01:40 +1000 Subject: [PATCH 7/9] Session 10 - you searched for text --- src/community_db/templates/base.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index f2ed587..f0d8871 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,8 +5,12 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- Search: + Search:
+ {% if search_text %} + Search results for: {{ search_text }} + {% endif %} +
{% block content %}{% endblock %} From 4a8044623f2fe155bbcf94bf6da46d390d3d3274 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 12:10:05 +1000 Subject: [PATCH 8/9] Session 10 - you searched for text in CBV --- src/community_db/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/community_db/views.py b/src/community_db/views.py index 79d176f..56ebb50 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -50,6 +50,17 @@ def get_queryset(self): # Return the queryset now that we have filtered it (if we need to) return queryset + def get_context_data(self, **kwargs): + # Get the default context that would be generated + context = super().get_context_data(**kwargs) + + # Get the search text and add it to the context + search_text = self.request.GET.get("search") + context["search_text"] = search_text + + # Return our new context + return context + class PersonDetailView(DetailView): model = Person From 67997f4f05da954a2ce1a80fb9fdf25f87e587e7 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:59:15 +1000 Subject: [PATCH 9/9] Session 10 - Django form --- src/community_db/forms.py | 5 +++++ src/community_db/templates/base.html | 3 ++- src/community_db/views.py | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 src/community_db/forms.py diff --git a/src/community_db/forms.py b/src/community_db/forms.py new file mode 100644 index 0000000..726140d --- /dev/null +++ b/src/community_db/forms.py @@ -0,0 +1,5 @@ +from django import forms + + +class QuickSearchForm(forms.Form): + search = forms.CharField(max_length=100, required=False) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index f0d8871..666ebf8 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,7 +5,8 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- Search: + {{ form.as_p }} +
{% if search_text %} Search results for: {{ search_text }} diff --git a/src/community_db/views.py b/src/community_db/views.py index 56ebb50..e46dccc 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -2,21 +2,25 @@ from django.shortcuts import render from django.views.generic import DetailView, ListView +from .forms import QuickSearchForm from .models import Person # FUNCTION BASED VIEWS -# Searching the first name and last name fields with text in the search box +# Searching the first name and last name fields using a Django form def list_persons_with_template(request): - search_text = request.GET.get("search") + form = QuickSearchForm(request.GET) persons = Person.objects.all() - if search_text: - search_filters = models.Q(first_name__icontains=search_text) | models.Q( - last_name__icontains=search_text - ) - persons = persons.filter(search_filters) - context = {"object_list": persons, "search_text": search_text} + search_text = "" + if form.is_valid(): + search_text = form.cleaned_data["search"] + if search_text: + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + persons = persons.filter(search_filters) + context = {"object_list": persons, "search_text": search_text, "form": form} return render(request, "community_db/person_list_in_base.html", context)