以下のように複数のフォーム(ドロップダウンリスト)があって、いずれかのドロップダウンリストの値が変更されたらsubmitするようなコードを書いています。python側でsubmitされた値を受け取る時に、どのドロップダウンリストの値が変わってsubmitされたのかを知りたい場合にはどうしたら良いでしょうか?
python
1from flask import Flask, render_template, request 2from wtforms import Form, validators, SelectField 3 4app = Flask(__name__) 5class CustomForm(Form): 6 line = SelectField(u'沿線', choices=[("山手線","山手線"),("丸ノ内線","丸ノ内線")]) 7 station = SelectField(u'駅', choices=[("東京","東京"),("渋谷","渋谷")]) 8 9@app.route('/', methods=['GET','POST']) 10def index(): 11 form = CustomForm(request.form) 12 selected_line = request.form.get("line") 13 selected_station = request.form.get("station") 14 print(selected_line, selected_station) 15 return render_template("index.html", form=form) 16
html
1<!doctype html> 2<html> 3 <head> 4 <title>demo</title> 5 </head> 6 <body> 7 {% block content %} 8 <form method="POST" action="/"> 9 {{ form.line.label }} {{ form.line(size=1, onchange="this.form.submit()") }} 10 {{ form.station.label }} {{ form.station(size=1, onchange="this.form.submit()") }} 11 </form> 12 {% endblock %} 13 </body> 14</html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/27 14:06