Skip to content

Commit

Permalink
Update for session 8 content
Browse files Browse the repository at this point in the history
  • Loading branch information
wsot committed Aug 18, 2022
1 parent 319e8cf commit 14560c8
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 3 deletions.
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)
3 changes: 2 additions & 1 deletion src/community_db/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.db import models


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)
9 changes: 9 additions & 0 deletions src/community_db/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<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
{% block content %}{% endblock %}
</body>

</html>
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>
11 changes: 11 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,11 @@
{% extends "base.html" %}

{% block content %}
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>
{% endblock %}
40 changes: 39 additions & 1 deletion src/community_db/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader
from django.views.generic.list import ListView

# Create your views here.
from community_db.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))


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


# def list_persons_with_template(request):
# persons = Person.objects.all()
# context = {"object_list": persons}
# return render(request, "community_db/person_list.html", context)


class PersonListView(ListView):
model = Person
template_name = "community_db/person_list_in_base.html"
5 changes: 5 additions & 0 deletions src/pacificconnect/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
from django.contrib import admin
from django.urls import path

from community_db import views

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()),
]

0 comments on commit 14560c8

Please sign in to comment.