回答編集履歴

2

修正

2020/05/29 06:47

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -1,35 +1,3 @@
1
- ```HTML
2
-
3
- <h1>{{category.name}}</h1>
4
-
5
- <p>{{category.description}}</p>
6
-
7
- ```でいけます。```{{category}}```のみだと、adminのCategory一覧に表示されるところしか表示されません。あなたは、models.pyで
8
-
9
- ```Python
10
-
11
- def __str__(self):
12
-
13
- self.name
14
-
15
- ```と設定していますよね。だから名前だけが表示されます。
16
-
17
-
18
-
19
- もし、
20
-
21
- ```Python
22
-
23
- def __str__(self):
24
-
25
- self.name + self.description
26
-
27
- ```にしたら、```{{category}}```のみでいけます。
28
-
29
-
30
-
31
- ---追記---
32
-
33
1
  views.pyを
34
2
 
35
3
  ```Python
@@ -48,4 +16,12 @@
48
16
 
49
17
  return render(request, "blog_category.html", context)
50
18
 
51
- ```こうしてください。
19
+ ```こうしてください。で、HTMLを
20
+
21
+ ```HTML
22
+
23
+ <h1>{{category.name}}</h1>
24
+
25
+ <p>{{category.description}}</p>
26
+
27
+ ```でいけます。

1

修正

2020/05/29 06:47

投稿

ForestSeo
ForestSeo

スコア2720

test CHANGED
@@ -25,3 +25,27 @@
25
25
  self.name + self.description
26
26
 
27
27
  ```にしたら、```{{category}}```のみでいけます。
28
+
29
+
30
+
31
+ ---追記---
32
+
33
+ views.pyを
34
+
35
+ ```Python
36
+
37
+ def blog_category(request, category):
38
+
39
+ posts = Post.objects.filter(categories__name__contains=category).order_by(
40
+
41
+ "-created_on"
42
+
43
+ )
44
+
45
+ category = Category.objects.get(name=category)
46
+
47
+ context = {"category": category, "posts": posts,}
48
+
49
+ return render(request, "blog_category.html", context)
50
+
51
+ ```こうしてください。