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/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/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..666ebf8 --- /dev/null +++ b/src/community_db/templates/base.html @@ -0,0 +1,18 @@ + + + +

Welcome to the Pacific Connect Community Database

+ On this site, you can find details of members of the Pacific Connect Community Database +
+
+ {{ form.as_p }} + +
+ {% if search_text %} + Search results for: {{ search_text }} + {% endif %} +
+ {% block content %}{% endblock %} + + + \ No newline at end of file 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.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..d9d4ede --- /dev/null +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -0,0 +1,14 @@ +{% 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..e46dccc 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,71 @@ +from django.db import models from django.shortcuts import render +from django.views.generic import DetailView, ListView -# Create your views here. +from .forms import QuickSearchForm +from .models import Person + +# FUNCTION BASED VIEWS + +# Searching the first name and last name fields using a Django form +def list_persons_with_template(request): + form = QuickSearchForm(request.GET) + + persons = Person.objects.all() + 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) + + +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" + + 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 + + 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 + template_name = "community_db/person_detail_in_base.html" diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index 5a454ad..d602477 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -16,6 +16,20 @@ from django.contrib import admin from django.urls import path +from community_db import views + urlpatterns = [ path("admin/", admin.site.urls), + 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", + ), ]