Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 9 to Session 10 #10

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
18 changes: 18 additions & 0 deletions src/community_db/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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>
{{ form.as_p }}
<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>
14 changes: 14 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,14 @@
{% extends "base.html" %}

{% block content %}
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>
{% endblock %}
70 changes: 69 additions & 1 deletion src/community_db/views.py
Original file line number Diff line number Diff line change
@@ -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"
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",
),
]