Q&A
前提
vscodeでpythonとsqlliteを使用して、Webアプリケーションを作成しています
実現したいこと
ここに実現したいことを箇条書きで書いてください。
HTMLから受け取ったカレンダーの日付をpythonで受け取って、sqlliteに登録したいです。
発生している問題・エラーメッセージ
データの型が違うのか、登録できません。
エラーメッセージ BadRequestKeyError werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'time' ### 該当のソースコード import sqlite3 from flask import Flask,render_template,request,g app = Flask(__name__) def get_db(): if 'db' not in g: # データベースをオープンしてFlaskのグローバル変数に保存 g.db = sqlite3.connect('LoginDB.db') return g.db @app.route('/') def index(): # データベースを開く con = get_db() # テーブル「商品一覧」の有無を確認 cur = con.execute("select count(*) from sqlite_master where TYPE='table' AND name='日付送信'") for row in cur: if row[0] == 0: # テーブル「商品一覧」がなければ作成する cur.execute("CREATE TABLE 商品一覧(コード INTEGER PRIMARY KEY, 商品名 STRING, 日付 int)") # レコードを作る # 商品一覧を読み込み cur = con.execute("select * from 商品一覧 order by コード") data = cur.fetchall() con.close() return render_template('index.html', data = data) @app.route('/result', methods=["POST"]) def result_post(): # テンプレートから新規登録する商品名と値段を取得 name = request.form["name"] time = request.form["time"] # データベースを開く con = get_db() # コードは既に登録されているコードの最大値+1の値で新規登録を行う cur = con.execute("select MAX(コード) AS max_code from 商品一覧") for row in cur: new_code = row[0] + 1 cur.close() # 登録処理 sql = "INSERT INTO 商品一覧(コード, 商品名, 値段)values({},'{}',{})".format(new_code, name, time) con.execute(sql) con.commit() # 一覧再読み込み cur = con.execute("select * from 商品一覧 order by コード") data = cur.fetchall() con.close() return render_template('index.html', data = data) if __name__ == '__main__': app.debug = True app.run(host='localhost') python HTML <!DOCTYPE html> <html lang="jp"> <head> <meta charset="UTF-8"> <title>商品一覧ページ</title> </head> <body> <div> <table> <thead> <tr> <th>コード</th> <th>商品名</th> <th>時間</th> </tr> </thead> <tbody> {% for item in data %} <tr> <th>{{item[0]}}</th> <th>{{item[1]}}</th> <th>{{item[2]}}</th> </tr> {% endfor %} </tbody> </table> </div> <form action='/result'> <p><新規登録></p> <label> 商品名 <input type="text" name="name"> </label> <label > 時間 <input type="date" name="time" max="9999-12-31"> </label> <button type="submit" formmethod="POST">登録</button> </form> </body> </html> ソースコード
時間
<input type="date" name="calendar" max="9999-12-31" name="time">
試したこと
ここに問題に対して試したことを記載してください。
データの型変更
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/10/31 03:28
2022/10/31 03:29
2022/10/31 03:30
2022/11/01 01:14
2022/11/01 01:26 編集