Skip to content

Commit db72024

Browse files
committed
Content for session 9. Tidy up URLs and Views.
1 parent 13f302e commit db72024

4 files changed

Lines changed: 45 additions & 31 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<br>
5+
<a href="{% url 'fbv-person-list' %}">Back to list</a>
6+
<br>
7+
This is the detail of a person:
8+
<ul>
9+
<li>First Name: {{ object.first_name }}</li>
10+
<li>Last Name: {{ object.last_name }}</li>
11+
<li>Country: {{ object.country }}</li>
12+
<li>Phone number: {{ object.mobile_number }}</li>
13+
</ul>
14+
{% endblock %}

src/community_db/templates/community_db/person_list_in_base.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
This is my list of folks
55
<ul>
66
{% for person in object_list %}
7-
<li>{{ person.first_name }} {{ person.last_name }}
8-
from {{ person.country }}</li>
7+
<li>
8+
<a href="{% url 'fbv-person-detail' person.id %}">
9+
{{ person.first_name }} {{ person.last_name }}
10+
</a> from {{ person.country }}
11+
</li>
912
{% endfor %}
1013
</ul>
1114
{% endblock %}

src/community_db/views.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
1-
from django.http import HttpResponse
21
from django.shortcuts import render
3-
from django.template import loader
4-
from django.views.generic.list import ListView
2+
from django.views.generic import DetailView, ListView
53

6-
from community_db.models import Person
4+
from .models import Person
75

8-
9-
def list_persons(request):
10-
html = "<html><body>This is my list of folks<ul>"
11-
12-
for person in Person.objects.all():
13-
html += f"<li>{person.first_name} {person.last_name} from {person.country}</li>"
14-
15-
html += "</ul></body></html>"
16-
return HttpResponse(html)
17-
18-
19-
# def list_persons_with_template(request):
20-
# persons = Person.objects.all()
21-
# template = loader.get_template("community_db/person_list.html")
22-
# context = {"object_list": persons}
23-
# return HttpResponse(template.render(context, request))
6+
# FUNCTION BASED VIEWS
247

258

269
def list_persons_with_template(request):
2710
persons = Person.objects.all()
28-
template = loader.get_template("community_db/person_list_in_base.html")
2911
context = {"object_list": persons}
30-
return HttpResponse(template.render(context, request))
12+
return render(request, "community_db/person_list_in_base.html", context)
3113

3214

33-
# def list_persons_with_template(request):
34-
# persons = Person.objects.all()
35-
# context = {"object_list": persons}
36-
# return render(request, "community_db/person_list.html", context)
15+
def detail_person_with_template(request, pk):
16+
person = Person.objects.get(id=pk)
17+
context = {"object": person}
18+
return render(request, "community_db/person_detail_in_base.html", context)
3719

3820

21+
# CLASS BASED VIEWS
3922
class PersonListView(ListView):
4023
model = Person
4124
template_name = "community_db/person_list_in_base.html"
25+
26+
27+
class PersonDetailView(DetailView):
28+
model = Person
29+
template_name = "community_db/person_detail_in_base.html"

src/pacificconnect/urls.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@
2020

2121
urlpatterns = [
2222
path("admin/", admin.site.urls),
23-
path("myview", views.list_persons),
24-
path("myview-with-template/", views.list_persons_with_template),
25-
path("person-list/", views.PersonListView.as_view()),
23+
path("fbv/people/", views.list_persons_with_template, name="fbv-person-list"),
24+
path(
25+
"fbv/people/<int:pk>/",
26+
views.detail_person_with_template,
27+
name="fbv-person-detail",
28+
),
29+
path("cbv/people/", views.PersonListView.as_view(), name="cbv-person-list"),
30+
path(
31+
"cbv/people/<int:pk>/",
32+
views.PersonDetailView.as_view(),
33+
name="cbv-person-detail",
34+
),
2635
]

0 commit comments

Comments
 (0)