|
1 | | -from django.http import HttpResponse |
2 | 1 | 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 |
5 | 3 |
|
6 | | -from community_db.models import Person |
| 4 | +from .models import Person |
7 | 5 |
|
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 |
24 | 7 |
|
25 | 8 |
|
26 | 9 | def list_persons_with_template(request): |
27 | 10 | persons = Person.objects.all() |
28 | | - template = loader.get_template("community_db/person_list_in_base.html") |
29 | 11 | context = {"object_list": persons} |
30 | | - return HttpResponse(template.render(context, request)) |
| 12 | + return render(request, "community_db/person_list_in_base.html", context) |
31 | 13 |
|
32 | 14 |
|
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) |
37 | 19 |
|
38 | 20 |
|
| 21 | +# CLASS BASED VIEWS |
39 | 22 | class PersonListView(ListView): |
40 | 23 | model = Person |
41 | 24 | 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" |
0 commit comments