前提・実現したいこと
pythonを勉強して1か月も経ってないくらいでまだよく知らないので質問です。
自分で調べて以下の書き方なら動く、というものは見つけたのですが、
これ以外にも同じ動作をする書き方があれば教えていただきたいです。
以下のコードは、例として3つのボタンを用意し、
それぞれ「12345」「67890」「1234567890」と表示します。(問題なく動いてます)
該当のソースコード
python
from flask import Flask, render_template, request app = Flask(__name__) @app.route("/", methods=["GET", "POST"]) def index(): if request.method == 'POST': if request.form['send'] == 'aaa': m = '12345' return render_template('index.html', message=m) if request.form['send'] == 'bbb': m = '67890' return render_template('index.html', message=m) if request.form['send'] == 'ccc': m = '1234567890' return render_template('index.html', message=m) else: return render_template('index.html') if __name__ == "__main__": app.run(host = '0.0.0.0', port = '5000')
html
<!DOCTYPE html> <html> <head> </head> <body> <p>{{ message }}</p> <form method="POST"> <input type="submit" name="send" value="aaa"> <input type="submit" name="send" value="bbb"> <input type="submit" name="send" value="ccc"> </form> </body> </html>
補足情報(FW/ツールのバージョンなど)
python 3.8.3
まだ回答がついていません
会員登録して回答してみよう