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 8 to Session 9 #9

Open
wants to merge 2 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)
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)
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>
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 %}
28 changes: 27 additions & 1 deletion src/community_db/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
from django.shortcuts import render
from django.views.generic import DetailView, ListView

# Create your views here.
from .models import Person

# FUNCTION BASED VIEWS


def list_persons_with_template(request):
persons = Person.objects.all()
context = {"object_list": persons}
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"


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