質問編集履歴
5
コード化
test
CHANGED
File without changes
|
test
CHANGED
@@ -130,6 +130,8 @@
|
|
130
130
|
|
131
131
|
|
132
132
|
|
133
|
+
```ここに言語を入力
|
134
|
+
|
133
135
|
(index.html)
|
134
136
|
|
135
137
|
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
|
@@ -150,9 +152,11 @@
|
|
150
152
|
|
151
153
|
</div>
|
152
154
|
|
155
|
+
```
|
153
156
|
|
154
157
|
|
155
158
|
|
159
|
+
```ここに言語を入力
|
156
160
|
|
157
161
|
(views.py)
|
158
162
|
|
@@ -187,3 +191,5 @@
|
|
187
191
|
}
|
188
192
|
|
189
193
|
return render(request, 'myapp/index.html', context)
|
194
|
+
|
195
|
+
```
|
4
内容追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -119,3 +119,71 @@
|
|
119
119
|
'with' expected at least one variable assignment
|
120
120
|
|
121
121
|
というエラーが出てきており,詰まっています.
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
(追加)
|
128
|
+
|
129
|
+
以下の通りにしてみたのですが,並び順が変わりませんでした.
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
(index.html)
|
134
|
+
|
135
|
+
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
|
136
|
+
|
137
|
+
aria-haspopup="true" aria-expanded="false">並替</a>
|
138
|
+
|
139
|
+
<div class="dropdown-menu dropdown-primary" aria-labelledby="navbarDropdownMenuLink">
|
140
|
+
|
141
|
+
<form action='' method='POST'>
|
142
|
+
|
143
|
+
{% csrf_token %}
|
144
|
+
|
145
|
+
<input type='submit' name='asc'>昇順</p>
|
146
|
+
|
147
|
+
<input type='submit' name='desc'>降順</p>
|
148
|
+
|
149
|
+
</form>
|
150
|
+
|
151
|
+
</div>
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
(views.py)
|
158
|
+
|
159
|
+
def Index(request):
|
160
|
+
|
161
|
+
post_list_asc = Post.objects.all()
|
162
|
+
|
163
|
+
post_list_desc = Post.objects.all().reverse()
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
if request.POST.get('name')=='asc':
|
168
|
+
|
169
|
+
post_list = post_list_asc
|
170
|
+
|
171
|
+
context = {
|
172
|
+
|
173
|
+
'post_list': post_list,
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
return render(request, 'myapp/index.html', context)
|
178
|
+
|
179
|
+
else:
|
180
|
+
|
181
|
+
post_list = post_list_desc
|
182
|
+
|
183
|
+
context = {
|
184
|
+
|
185
|
+
'post_list': post_list,
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
return render(request, 'myapp/index.html', context)
|
3
内容変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
djangoのテンプレート
|
1
|
+
djangoのテンプレートの変数をプルダウンで切り替えたい
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# djangoのテンプレート
|
1
|
+
# djangoのテンプレートの変数をプルダウンで切り替えたい
|
2
2
|
|
3
3
|
djangoのバージョン:3.1.7
|
4
4
|
|
2
誤字修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,9 +92,9 @@
|
|
92
92
|
|
93
93
|
{% csrf_token %}
|
94
94
|
|
95
|
-
<input type='submit' {% with ordering = post_list %}{% endwith %}>{{ ordering }}</p>
|
95
|
+
<input type='submit' {% with ordering = post_list_asc %}{% endwith %}>{{ ordering }}</p>
|
96
96
|
|
97
|
-
<input type='submit' {% with ordering = post_list_
|
97
|
+
<input type='submit' {% with ordering = post_list_desc %}{% endwith %}>{{ ordering }}</p>
|
98
98
|
|
99
99
|
</form>
|
100
100
|
|
1
内容追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,7 +44,13 @@
|
|
44
44
|
|
45
45
|
{% for item in post_list_desc %}
|
46
46
|
|
47
|
+
```
|
48
|
+
|
47
49
|
または
|
50
|
+
|
51
|
+
```ここに言語を入力
|
52
|
+
|
53
|
+
(index.html)
|
48
54
|
|
49
55
|
{% for item in post_list_asc %}
|
50
56
|
|
@@ -63,3 +69,53 @@
|
|
63
69
|
```
|
64
70
|
|
65
71
|
のようにして,プルダウンで昇順・降順を切り替える(変数を切り替える)方法が思いつかないので,お分かりの方はお教えいただければ助かります.
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
試しにhttps://hodalog.com/how-to-set-a-value-of-a-variable-inside-a-template-code/を参考に
|
76
|
+
|
77
|
+
```ここに言語を入力
|
78
|
+
|
79
|
+
(index.html)
|
80
|
+
|
81
|
+
<div class="row">
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
|
86
|
+
|
87
|
+
aria-haspopup="true" aria-expanded="false">並替</a>
|
88
|
+
|
89
|
+
<div class="dropdown-menu dropdown-primary" aria-labelledby="navbarDropdownMenuLink">
|
90
|
+
|
91
|
+
<form action='' method='POST'>
|
92
|
+
|
93
|
+
{% csrf_token %}
|
94
|
+
|
95
|
+
<input type='submit' {% with ordering = post_list %}{% endwith %}>{{ ordering }}</p>
|
96
|
+
|
97
|
+
<input type='submit' {% with ordering = post_list_reverse %}{% endwith %}>{{ ordering }}</p>
|
98
|
+
|
99
|
+
</form>
|
100
|
+
|
101
|
+
</div>
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
<!-- Card deck -->
|
106
|
+
|
107
|
+
<div class="card-deck">
|
108
|
+
|
109
|
+
{% for item in ordering %}
|
110
|
+
|
111
|
+
(以下省略)
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
としてみたら,
|
116
|
+
|
117
|
+
TemplateSyntaxError at /
|
118
|
+
|
119
|
+
'with' expected at least one variable assignment
|
120
|
+
|
121
|
+
というエラーが出てきており,詰まっています.
|