Skip to content

Commit 13f302e

Browse files
committed
Update for session 8 content
1 parent 93990d1 commit 13f302e

7 files changed

Lines changed: 89 additions & 3 deletions

File tree

src/community_db/admin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
from django.contrib import admin
22

3-
# Register your models here.
3+
from .models import Person
4+
5+
6+
class PersonAdmin(admin.ModelAdmin):
7+
list_display = (
8+
"first_name",
9+
"last_name",
10+
)
11+
12+
13+
admin.site.register(Person, PersonAdmin)

src/community_db/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
class Person(models.Model):
55
first_name = models.CharField(max_length=100)
6-
last_name = models.CharField(max_length=100, blank=True)
6+
last_name = models.CharField(max_length=100)
77
country = models.CharField(max_length=100, blank=True)
88
mobile_number = models.CharField(max_length=20, blank=True)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
3+
<body>
4+
<h1>Welcome to the Pacific Connect Community Database</h1>
5+
On this site, you can find details of members of the Pacific Connect Community Database
6+
{% block content %}{% endblock %}
7+
</body>
8+
9+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
3+
<body>
4+
This is my list of folks
5+
<ul>
6+
{% for person in object_list %}
7+
<li>{{ person.first_name }} {{ person.last_name }}
8+
from {{ person.country }}</li>
9+
{% endfor %}
10+
</ul>
11+
</body>
12+
13+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
This is my list of folks
5+
<ul>
6+
{% for person in object_list %}
7+
<li>{{ person.first_name }} {{ person.last_name }}
8+
from {{ person.country }}</li>
9+
{% endfor %}
10+
</ul>
11+
{% endblock %}

src/community_db/views.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
from django.http import HttpResponse
12
from django.shortcuts import render
3+
from django.template import loader
4+
from django.views.generic.list import ListView
25

3-
# Create your views here.
6+
from community_db.models import Person
7+
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))
24+
25+
26+
def list_persons_with_template(request):
27+
persons = Person.objects.all()
28+
template = loader.get_template("community_db/person_list_in_base.html")
29+
context = {"object_list": persons}
30+
return HttpResponse(template.render(context, request))
31+
32+
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)
37+
38+
39+
class PersonListView(ListView):
40+
model = Person
41+
template_name = "community_db/person_list_in_base.html"

src/pacificconnect/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
from django.contrib import admin
1717
from django.urls import path
1818

19+
from community_db import views
20+
1921
urlpatterns = [
2022
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()),
2126
]

0 commit comments

Comments
 (0)