Skip to content

Commit

Permalink
Content for session 9. Tidy up URLs and Views.
Browse files Browse the repository at this point in the history
  • Loading branch information
wsot committed Sep 19, 2022
1 parent 13f302e commit db72024
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
This is my list of folks
<ul>
{% for person in object_list %}
<li>{{ person.first_name }} {{ person.last_name }}
from {{ person.country }}</li>
<li>
<a href="{% url 'fbv-person-detail' person.id %}">
{{ person.first_name }} {{ person.last_name }}
</a> from {{ person.country }}
</li>
{% endfor %}
</ul>
{% endblock %}
40 changes: 14 additions & 26 deletions src/community_db/views.py
Original file line number Diff line number Diff line change
@@ -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 = "<html><body>This is my list of folks<ul>"

for person in Person.objects.all():
html += f"<li>{person.first_name} {person.last_name} from {person.country}</li>"

html += "</ul></body></html>"
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"
15 changes: 12 additions & 3 deletions src/pacificconnect/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<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",
),
]

0 comments on commit db72024

Please sign in to comment.