回答編集履歴
4
回答を修正
test
CHANGED
@@ -50,21 +50,29 @@
|
|
50
50
|
|
51
51
|
if pk:
|
52
52
|
|
53
|
-
try:
|
54
|
-
|
55
|
-
id = int(pk)
|
56
|
-
|
57
|
-
|
53
|
+
ctx['current_book'] = get_object_or_404(Book, pk=int(id))
|
58
|
-
|
59
|
-
except ValueError:
|
60
|
-
|
61
|
-
return HttpResponseBadRequest()
|
62
54
|
|
63
55
|
else:
|
64
56
|
|
65
57
|
ctx['current_book'] = ctx['object_list'].first()
|
66
58
|
|
67
59
|
return ctx
|
60
|
+
|
61
|
+
def get(self, request, *args, **kwargs):
|
62
|
+
|
63
|
+
pk = self.request.GET.get('pk', None)
|
64
|
+
|
65
|
+
if pk:
|
66
|
+
|
67
|
+
try:
|
68
|
+
|
69
|
+
_ = int(pk)
|
70
|
+
|
71
|
+
except ValueError:
|
72
|
+
|
73
|
+
return HttpResponseBadRequest()
|
74
|
+
|
75
|
+
return super().get(request, *args, **kwargs)
|
68
76
|
|
69
77
|
|
70
78
|
|
3
サンプルの改善
test
CHANGED
@@ -42,21 +42,27 @@
|
|
42
42
|
|
43
43
|
model = Book
|
44
44
|
|
45
|
-
|
46
|
-
|
47
45
|
def get_context_data(self, **kwargs):
|
48
46
|
|
49
|
-
|
47
|
+
ctx = super().get_context_data(**kwargs)
|
50
48
|
|
51
49
|
pk = self.request.GET.get('pk', None)
|
52
50
|
|
53
|
-
if pk
|
51
|
+
if pk:
|
54
52
|
|
55
|
-
|
53
|
+
try:
|
56
54
|
|
57
|
-
|
55
|
+
id = int(pk)
|
58
56
|
|
59
|
-
ctx['current_book'] = get_object_or_404(Book, pk=
|
57
|
+
ctx['current_book'] = get_object_or_404(Book, pk=id)
|
58
|
+
|
59
|
+
except ValueError:
|
60
|
+
|
61
|
+
return HttpResponseBadRequest()
|
62
|
+
|
63
|
+
else:
|
64
|
+
|
65
|
+
ctx['current_book'] = ctx['object_list'].first()
|
60
66
|
|
61
67
|
return ctx
|
62
68
|
|
2
サンプルの修正
test
CHANGED
@@ -68,7 +68,7 @@
|
|
68
68
|
|
69
69
|
urlpatterns = [
|
70
70
|
|
71
|
-
path('', BookList.as_view, name='book-list'),
|
71
|
+
path('', BookList.as_view(), name='book-list'),
|
72
72
|
|
73
73
|
]
|
74
74
|
|
1
サンプルを追加
test
CHANGED
@@ -7,3 +7,113 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
アプリ固有の仕様を満たすビューは、ListViewやDetailViewを上記で提示したようにカスタマイズするか、TempleteViewで実装します。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
(ミスがあるかもしれませんが・・・)下記のように実装すれば、下に示したURLで詳細とリストが表示されるはずです。
|
14
|
+
|
15
|
+
URL: http://127.0.0.1:8000/books/?pk=1
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
```python
|
20
|
+
|
21
|
+
# in books/models.py
|
22
|
+
|
23
|
+
from django.db import models
|
24
|
+
|
25
|
+
class Book(models.Model):
|
26
|
+
|
27
|
+
title = models.CharField(max_length=80)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
# in books/views.py
|
32
|
+
|
33
|
+
from django.views import generic
|
34
|
+
|
35
|
+
from django.http.response import HttpResponseBadRequest
|
36
|
+
|
37
|
+
from django.shortcuts import get_object_or_404
|
38
|
+
|
39
|
+
from .models import Book
|
40
|
+
|
41
|
+
class BookList(generic.ListView):
|
42
|
+
|
43
|
+
model = Book
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
def get_context_data(self, **kwargs):
|
48
|
+
|
49
|
+
"""GETパラメーターのpkをidに持つBookが存在しな場合は404"""
|
50
|
+
|
51
|
+
pk = self.request.GET.get('pk', None)
|
52
|
+
|
53
|
+
if pk is None:
|
54
|
+
|
55
|
+
return HttpResponseBadRequest()
|
56
|
+
|
57
|
+
ctx = super().get_context_data(**kwargs)
|
58
|
+
|
59
|
+
ctx['current_book'] = get_object_or_404(Book, pk=pk)
|
60
|
+
|
61
|
+
return ctx
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
# in books/urls.py
|
66
|
+
|
67
|
+
from .views import BookList
|
68
|
+
|
69
|
+
urlpatterns = [
|
70
|
+
|
71
|
+
path('', BookList.as_view, name='book-list'),
|
72
|
+
|
73
|
+
]
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
# in <project>/urls.py
|
78
|
+
|
79
|
+
from django.urls import path, include
|
80
|
+
|
81
|
+
urlpatterns = [
|
82
|
+
|
83
|
+
path('books/',include("books.urls")),
|
84
|
+
|
85
|
+
]
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
```html
|
92
|
+
|
93
|
+
# in books/templates/books/book_list.html
|
94
|
+
|
95
|
+
<html>
|
96
|
+
|
97
|
+
<body>
|
98
|
+
|
99
|
+
<div>{{ current_book.title }}</div>
|
100
|
+
|
101
|
+
<div>
|
102
|
+
|
103
|
+
<table>
|
104
|
+
|
105
|
+
{% for book in object_list %}
|
106
|
+
|
107
|
+
<tr><td>{{ book.title }}</td></tr>
|
108
|
+
|
109
|
+
{% endfor }
|
110
|
+
|
111
|
+
</table>
|
112
|
+
|
113
|
+
</div>
|
114
|
+
|
115
|
+
</body>
|
116
|
+
|
117
|
+
</html>
|
118
|
+
|
119
|
+
```
|