質問編集履歴
3
コード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,7 +9,8 @@
|
|
9
9
|
amount = db.Column(db.String(20), nullable = True)
|
10
10
|
|
11
11
|
〜中略〜
|
12
|
-
|
12
|
+
@app.route('/shopping_edit', methods=['POST'])
|
13
|
+
@login_required
|
13
14
|
def shopping_data():
|
14
15
|
# formでチェックが入っているアイテムのidだけを取得している
|
15
16
|
id_shopping_list = request.form.getlist('id_list')
|
2
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -41,7 +41,7 @@
|
|
41
41
|
|
42
42
|
return render_template('show_shopping_list.html',shopping_list = shopping_list )
|
43
43
|
```
|
44
|
-
```HTML
|
44
|
+
```HTML
|
45
45
|
<form action="/shopping_edit" method="post" enctype="multipart/form-data">
|
46
46
|
{% for data in ShoppingList_data %}
|
47
47
|
<div class="row">
|
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,27 +12,25 @@
|
|
12
12
|
|
13
13
|
def shopping_data():
|
14
14
|
# formでチェックが入っているアイテムのidだけを取得している
|
15
|
-
id_shopping_list = request.form.getlist('
|
15
|
+
id_shopping_list = request.form.getlist('id_list')
|
16
16
|
|
17
17
|
# id_shopping_listをint型の配列に変換
|
18
18
|
id_shopping_list_int =[]
|
19
19
|
for id in id_shopping_list:
|
20
20
|
id_shopping_list_int.append(int(id))
|
21
21
|
|
22
|
-
# データベースからチェックが入っていたアイテムだけを取得
|
22
|
+
# データベースからチェックが入っていたアイテムだけをデータベースから取得しておく
|
23
23
|
items =[]
|
24
24
|
for id in id_shopping_list_int:
|
25
25
|
items.append(db.session.query(ShoppingList).get(id+1))
|
26
|
-
|
27
|
-
# この段階ではShoppingListテーブルはメニューに使う全ての素材が登録されている状態なので、
|
28
|
-
# 上で準備したitemsだけをShoppingListに再登録する
|
29
26
|
|
30
27
|
# 一旦、ShoppingListテーブルを空にする
|
31
28
|
db.session.query(ShoppingList).delete()
|
32
29
|
db.session.commit()
|
30
|
+
|
33
31
|
# 空になったShoppingListテーブルに、itemsを再登録する
|
34
32
|
for i in range(len(items)):
|
35
|
-
item =
|
33
|
+
item = ShoppingList()
|
36
34
|
item.name = items[i].name
|
37
35
|
item.amount = items[i].amount
|
38
36
|
db.session.add(item)
|
@@ -42,4 +40,23 @@
|
|
42
40
|
shopping_list = db.session.query(ShoppingList).all()
|
43
41
|
|
44
42
|
return render_template('show_shopping_list.html',shopping_list = shopping_list )
|
43
|
+
```
|
44
|
+
```HTML(flask)
|
45
|
+
<form action="/shopping_edit" method="post" enctype="multipart/form-data">
|
46
|
+
{% for data in ShoppingList_data %}
|
47
|
+
<div class="row">
|
48
|
+
<div class="col-sm-1">
|
49
|
+
<input type="checkbox" name="id_list" value="{{ data.id }}">
|
50
|
+
</div>
|
51
|
+
<div class="col-sm-2">
|
52
|
+
{{ data.name }}
|
53
|
+
</div>
|
54
|
+
<div class="col-sm-2">
|
55
|
+
{{ data.amount }}
|
56
|
+
</div>
|
57
|
+
</div>
|
58
|
+
{% endfor %}
|
59
|
+
<button type="submit" class="btn btn-info" name = "action">保存する</button>
|
60
|
+
<a class="btn btn-info" href="/" role="button">キャンセル</a>
|
61
|
+
</form>
|
45
62
|
```
|