Skip to content
12 changes: 11 additions & 1 deletion src/community_db/admin.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions src/community_db/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django import forms


class QuickSearchForm(forms.Form):
search = forms.CharField(max_length=100, required=False)
2 changes: 1 addition & 1 deletion src/community_db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 19 additions & 0 deletions src/community_db/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>

<body>
<h1>Welcome to the Pacific Connect Community Database</h1>
On this site, you can find details of members of the Pacific Connect Community Database
<br>
<form action="{% url "fbv-person-list" %}">
<label for="id-search">Search:</label>
<input type="text" name="search" id="id-search">
<input type="submit">
</form>
{% if search_text %}
Search results for: {{ search_text }}
{% endif %}
<br>
{% block content %}{% endblock %}
</body>

</html>
14 changes: 14 additions & 0 deletions src/community_db/templates/community_db/person_detail_in_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}

{% block content %}
<br>
<a href="{% url 'fbv-person-list' %}">Back to list</a>
<br>
This is the detail of a person:
<ul>
<li>First Name: {{ object.first_name }}</li>
<li>Last Name: {{ object.last_name }}</li>
<li>Country: {{ object.country }}</li>
<li>Phone number: {{ object.mobile_number }}</li>
</ul>
{% endblock %}
13 changes: 13 additions & 0 deletions src/community_db/templates/community_db/person_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>

<body>
This is my list of folks
<ul>
{% for person in object_list %}
<li>{{ person.first_name }} {{ person.last_name }}
from {{ person.country }}</li>
{% endfor %}
</ul>
</body>

</html>
18 changes: 18 additions & 0 deletions src/community_db/templates/community_db/person_list_in_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block content %}
{% if object_list %}
This is my list of folks
<ul>
{% for person in object_list %}
<li>
<a href="{% url 'fbv-person-detail' person.id %}">
{{ person.first_name }} {{ person.last_name }}
</a> from {{ person.country }}
</li>
{% endfor %}
</ul>
{% else %}
No profiles match that search
{% endif %}
{% endblock %}
68 changes: 66 additions & 2 deletions src/community_db/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
from django.shortcuts import render
from django.db import models
from django.shortcuts import get_object_or_404, render
from django.views.generic import DetailView, ListView

# Create your views here.
from .models import Person

# FUNCTION BASED VIEWS

# 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")

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}
return render(request, "community_db/person_list_in_base.html", context)


def detail_person_with_template(request, pk):
person = get_object_or_404(Person, 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"
14 changes: 14 additions & 0 deletions src/pacificconnect/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:pk>/",
views.detail_person_with_template,
name="fbv-person-detail",
),
path("cbv/people/", views.PersonListView.as_view(), name="cbv-person-list"),
path(
"cbv/people/<int:pk>/",
views.PersonDetailView.as_view(),
name="cbv-person-detail",
),
]