質問編集履歴
3
コードを更新しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -49,3 +49,115 @@
|
|
49
49
|
return render(request, "blog_category.html", context)
|
50
50
|
|
51
51
|
```
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
下記コードを全記しました
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
model.py
|
60
|
+
|
61
|
+
```ここに言語を入力
|
62
|
+
|
63
|
+
from django.db import models
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
class Category(models.Model):
|
70
|
+
|
71
|
+
name = models.CharField(max_length=20)
|
72
|
+
|
73
|
+
description = models.TextField(default="")
|
74
|
+
|
75
|
+
def __str__(self):
|
76
|
+
|
77
|
+
return self.name + self.description
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
view.py
|
82
|
+
|
83
|
+
```ここに言語を入力
|
84
|
+
|
85
|
+
from django.shortcuts import render
|
86
|
+
|
87
|
+
from blog.models import Post, Category
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def blog_category(request, category):
|
92
|
+
|
93
|
+
posts = Post.objects.filter(categories__name__contains=category).order_by(
|
94
|
+
|
95
|
+
"-created_on"
|
96
|
+
|
97
|
+
)
|
98
|
+
|
99
|
+
context = {"category": category, "posts": posts,}
|
100
|
+
|
101
|
+
return render(request, "blog_category.html", context)
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
HTML
|
108
|
+
|
109
|
+
```ここに言語を入力
|
110
|
+
|
111
|
+
{% extends "base.html" %}
|
112
|
+
|
113
|
+
{% block page_content %}
|
114
|
+
|
115
|
+
<div class="col-md-8 offset-md-2">
|
116
|
+
|
117
|
+
<h1>{{ category | title }}</h1>
|
118
|
+
|
119
|
+
<p>{{category}}</p>
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
<hr>
|
124
|
+
|
125
|
+
{% for post in posts %}
|
126
|
+
|
127
|
+
<h2><a href="{% url 'blog_detail' post.pk%}">{{ post.title }}</a></h2>
|
128
|
+
|
129
|
+
<small>
|
130
|
+
|
131
|
+
{{ post.created_on.date }} |
|
132
|
+
|
133
|
+
Categories:
|
134
|
+
|
135
|
+
{% for category in post.categories.all %}
|
136
|
+
|
137
|
+
<a href="{% url 'blog_category' category.name %}">
|
138
|
+
|
139
|
+
{{ category.name }}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
</a>
|
144
|
+
|
145
|
+
{% endfor %}
|
146
|
+
|
147
|
+
</small>
|
148
|
+
|
149
|
+
<p>{{ post.body | slice:":400" }}...</p>
|
150
|
+
|
151
|
+
{% endfor %}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
</div>
|
158
|
+
|
159
|
+
{% endblock %}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
```
|
2
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,23 @@
|
|
29
29
|
どのようにすればnameとdescription両方を表示できるでしょうか?
|
30
30
|
|
31
31
|
{{ category }}とすればmodel categoryに登録されているものが表示される(今回の場合はname, description)という認識でしたが、間違っていますか?
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
ちなみにviewは下記のように設定しています。
|
36
|
+
|
37
|
+
```python
|
38
|
+
|
39
|
+
def blog_category(request, category):
|
40
|
+
|
41
|
+
posts = Post.objects.filter(categories__name__contains=category).order_by(
|
42
|
+
|
43
|
+
"-created_on"
|
44
|
+
|
45
|
+
)
|
46
|
+
|
47
|
+
context = {"category": category, "posts": posts,}
|
48
|
+
|
49
|
+
return render(request, "blog_category.html", context)
|
50
|
+
|
51
|
+
```
|
1
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,3 +27,5 @@
|
|
27
27
|
|
28
28
|
|
29
29
|
どのようにすればnameとdescription両方を表示できるでしょうか?
|
30
|
+
|
31
|
+
{{ category }}とすればmodel categoryに登録されているものが表示される(今回の場合はname, description)という認識でしたが、間違っていますか?
|