前提
Python(Flask)を使ってカフェシステムの構築しています。
実現したいこと
現在、複数の違う商品の購入したい個数を選択しそれをPOSTで送る。というところで躓いています。
発生している問題・エラーメッセージ
POSTでのデータの受け渡しがうまくできず 'None' と表示される。
該当のソースコード
index.py
1from flaskr import app 2from flask import render_template, request, redirect, url_for 3import sqlite3 4DATABASE = 'sql_test.db' 5 6@app.route('/') 7def index(): 8 con = sqlite3.connect(DATABASE) 9 db_products = con.execute('SELECT * FROM itemMaster').fetchall() 10 con.close() 11 12 products = [] 13 for row in db_products: 14 products.append({'itemID' : row[0], 'itemName' : row[1], 'itemPrice' : row[2], 'itemStock' : row[3]}) 15 16 return render_template( 17 'index.html', 18 products = products 19 ) 20 21@app.route('/order', methods=['POST']) 22def order(): 23 app.logger.debug(request.form.get('item1')) 24 return redirect(url_for("index"))
index.html
1<!DOCTYPE html> 2<html lang="ja"> 3 <head charset="UTF-8"> 4 <title>Kitchen</title> 5 <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> 6 </head> 7 <body> 8 <h1>Kitchen</h1> 9 <form method="post" action="{{ url_for('order') }}"> 10 <table border="1" style="border-collapse: collapse"> 11 {% if products == [] %} 12 <p>商品はありません</p> 13 {% else %} 14 <tr> 15 <th>商品ID</th> 16 <th>商品名</th> 17 <th>価格</th> 18 <th>在庫数</th> 19 <th>購入数</th> 20 </tr> 21 {% for product in products %} 22 <tr> 23 <td> {{ product.itemID }} </td> 24 <td> {{ product.itemName }} </td> 25 <td> {{ product.itemPrice }} </td> 26 <td> {{ product.itemStock }} </td> 27 <td> 28 <div class="spinner_area"> 29 <input type="button" value="-" class="btnspinner" data-cal="-1" data-target=".counter1"> 30 <input type="number" name=" {{ 'item' + product.itemID|string }} " value="0" class="counter1" data-max="500" data-min="0"> 31 <input type="button" value="+" class="btnspinner" data-cal="1" data-target=".counter1"> 32 </div> 33 </td> 34 </tr> 35 {% endfor %} 36 {% endif %} 37 </table> 38 <input type="submit" value="購入"> 39 </form> 40 </body> 41</html>
試したこと
inputタグのnameをitemIDにて振り分けることで、一意に識別できるようにしました。
初めは
main.py
1request.form['item1']
としていましたが、
400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
と出るため.getの方を使用しました。
デベロッパーツールでFormDataを確認すると
item1 : 2 item2 : 1 +item1+=2&+item2+=1(parsed)
と表示されています。
エラー文を検索にかけて自分なりに試しましたがわからないため質問させていただきます。
ご教示頂けましたら幸いです。よろしくお願い致します。