回答編集履歴
2
回答を追加
test
CHANGED
@@ -21,3 +21,45 @@
|
|
21
21
|
{% url 'app_name:spam' pk=item.pk prefecture_name=prefecture.name %}
|
22
22
|
|
23
23
|
```
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
都道府県でViewを分けたいのであれば、テンプレートタグでURLを取得する方法はどうでしょうか?
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```python
|
32
|
+
|
33
|
+
# app/templatetags/spam.py
|
34
|
+
|
35
|
+
from django import template
|
36
|
+
|
37
|
+
register = template.Library()
|
38
|
+
|
39
|
+
@register.simple_tag
|
40
|
+
|
41
|
+
def get_uri(item: Item, prefecture_name: str) -> str:
|
42
|
+
|
43
|
+
view_name = f'app:{prefecture_name}'
|
44
|
+
|
45
|
+
return reverse(view_name, kwargs={'pk': item.id,})
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```html
|
52
|
+
|
53
|
+
{% load spam %}
|
54
|
+
|
55
|
+
{% for item in items %}
|
56
|
+
|
57
|
+
{% for prefecture in prefectures %}
|
58
|
+
|
59
|
+
<a href="{% get_uri item prefecture %}">{{ item.name }} | {{ prefecture.name }}</a>
|
60
|
+
|
61
|
+
{% endfor %}
|
62
|
+
|
63
|
+
{% endfor %}
|
64
|
+
|
65
|
+
```
|
1
回答の修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
urlpatterns = [
|
10
10
|
|
11
|
-
path('item/<int:pk>/<str:pref_name>/', SpamView.as_view(), name='spam'),
|
11
|
+
path('item/<int:pk>/<str:prefecture_name>/', SpamView.as_view(), name='spam'),
|
12
12
|
|
13
13
|
]
|
14
14
|
|