質問するログイン新規登録

質問編集履歴

5

コード化

2021/05/24 12:13

投稿

bauchan
bauchan

スコア0

title CHANGED
File without changes
body CHANGED
@@ -64,6 +64,7 @@
64
64
  (追加)
65
65
  以下の通りにしてみたのですが,並び順が変わりませんでした.
66
66
 
67
+ ```ここに言語を入力
67
68
  (index.html)
68
69
  <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
69
70
  aria-haspopup="true" aria-expanded="false">並替</a>
@@ -74,8 +75,9 @@
74
75
  <input type='submit' name='desc'>降順</p>
75
76
  </form>
76
77
  </div>
78
+ ```
77
79
 
78
-
80
+ ```ここに言語を入力
79
81
  (views.py)
80
82
  def Index(request):
81
83
  post_list_asc = Post.objects.all()
@@ -92,4 +94,5 @@
92
94
  context = {
93
95
  'post_list': post_list,
94
96
  }
95
- return render(request, 'myapp/index.html', context)
97
+ return render(request, 'myapp/index.html', context)
98
+ ```

4

内容追加

2021/05/24 12:13

投稿

bauchan
bauchan

スコア0

title CHANGED
File without changes
body CHANGED
@@ -58,4 +58,38 @@
58
58
  としてみたら,
59
59
  TemplateSyntaxError at /
60
60
  'with' expected at least one variable assignment
61
- というエラーが出てきており,詰まっています.
61
+ というエラーが出てきており,詰まっています.
62
+
63
+
64
+ (追加)
65
+ 以下の通りにしてみたのですが,並び順が変わりませんでした.
66
+
67
+ (index.html)
68
+ <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
69
+ aria-haspopup="true" aria-expanded="false">並替</a>
70
+ <div class="dropdown-menu dropdown-primary" aria-labelledby="navbarDropdownMenuLink">
71
+ <form action='' method='POST'>
72
+ {% csrf_token %}
73
+ <input type='submit' name='asc'>昇順</p>
74
+ <input type='submit' name='desc'>降順</p>
75
+ </form>
76
+ </div>
77
+
78
+
79
+ (views.py)
80
+ def Index(request):
81
+ post_list_asc = Post.objects.all()
82
+ post_list_desc = Post.objects.all().reverse()
83
+
84
+ if request.POST.get('name')=='asc':
85
+ post_list = post_list_asc
86
+ context = {
87
+ 'post_list': post_list,
88
+ }
89
+ return render(request, 'myapp/index.html', context)
90
+ else:
91
+ post_list = post_list_desc
92
+ context = {
93
+ 'post_list': post_list,
94
+ }
95
+ return render(request, 'myapp/index.html', context)

3

内容変更

2021/05/24 12:12

投稿

bauchan
bauchan

スコア0

title CHANGED
@@ -1,1 +1,1 @@
1
- djangoのテンプレート変数を切り替えるためのプルダウンを設けたい
1
+ djangoのテンプレート変数をプルダウンで切り替えたい
body CHANGED
@@ -1,4 +1,4 @@
1
- # djangoのテンプレート変数を切り替えるためのプルダウンを設けたい
1
+ # djangoのテンプレート変数をプルダウンで切り替えたい
2
2
  djangoのバージョン:3.1.7
3
3
  pythonのバージョン:3.8.5
4
4
 

2

誤字修正

2021/05/22 08:27

投稿

bauchan
bauchan

スコア0

title CHANGED
File without changes
body CHANGED
@@ -45,8 +45,8 @@
45
45
  <div class="dropdown-menu dropdown-primary" aria-labelledby="navbarDropdownMenuLink">
46
46
  <form action='' method='POST'>
47
47
  {% csrf_token %}
48
- <input type='submit' {% with ordering = post_list %}{% endwith %}>{{ ordering }}</p>
48
+ <input type='submit' {% with ordering = post_list_asc %}{% endwith %}>{{ ordering }}</p>
49
- <input type='submit' {% with ordering = post_list_reverse %}{% endwith %}>{{ ordering }}</p>
49
+ <input type='submit' {% with ordering = post_list_desc %}{% endwith %}>{{ ordering }}</p>
50
50
  </form>
51
51
  </div>
52
52
 

1

内容追加

2021/05/22 08:22

投稿

bauchan
bauchan

スコア0

title CHANGED
File without changes
body CHANGED
@@ -21,7 +21,10 @@
21
21
  ```ここに言語を入力
22
22
  (index.html)
23
23
  {% for item in post_list_desc %}
24
+ ```
24
25
  または
26
+ ```ここに言語を入力
27
+ (index.html)
25
28
  {% for item in post_list_asc %}
26
29
  ```
27
30
  とするのではなく
@@ -30,4 +33,29 @@
30
33
  (index.html)
31
34
  {% for item in 変数 %} #条件に応じて変数をpost_list_descまたはpost_list_ascに切り替えられるようにする
32
35
  ```
33
- のようにして,プルダウンで昇順・降順を切り替える(変数を切り替える)方法が思いつかないので,お分かりの方はお教えいただければ助かります.
36
+ のようにして,プルダウンで昇順・降順を切り替える(変数を切り替える)方法が思いつかないので,お分かりの方はお教えいただければ助かります.
37
+
38
+ 試しにhttps://hodalog.com/how-to-set-a-value-of-a-variable-inside-a-template-code/を参考に
39
+ ```ここに言語を入力
40
+ (index.html)
41
+ <div class="row">
42
+
43
+ <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
44
+ aria-haspopup="true" aria-expanded="false">並替</a>
45
+ <div class="dropdown-menu dropdown-primary" aria-labelledby="navbarDropdownMenuLink">
46
+ <form action='' method='POST'>
47
+ {% csrf_token %}
48
+ <input type='submit' {% with ordering = post_list %}{% endwith %}>{{ ordering }}</p>
49
+ <input type='submit' {% with ordering = post_list_reverse %}{% endwith %}>{{ ordering }}</p>
50
+ </form>
51
+ </div>
52
+
53
+ <!-- Card deck -->
54
+ <div class="card-deck">
55
+ {% for item in ordering %}
56
+ (以下省略)
57
+ ```
58
+ としてみたら,
59
+ TemplateSyntaxError at /
60
+ 'with' expected at least one variable assignment
61
+ というエラーが出てきており,詰まっています.