Flask で入力フォームのデータを POST で受け渡してみた。
メモ
構成
- ディレクトリ構成
$ tree flask flask ├── hello.py └── templates ├── confirm.html └── form.html 1 directory, 3 files
- hello.py
from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def hello(): name = "Hello World" return name @app.route('/good') def good(): name = "Good" return name @app.route('/form') def form(): return render_template('form.html') @app.route('/confirm', methods = ['POST', 'GET']) def confirm(): if request.method == 'POST': result = request.form return render_template("confirm.html",result = result) if __name__ == "__main__": app.run(host='0.0.0.0', debug=True)
- templates/form.html
<html> <body> <form action = "/confirm" method = "POST"> <p>Name <input type = "text" name = "Name" /></p> <p>Value <input type ="text" name = "Value" /></p> <p><input type = "submit" value = "submit" /></p> </form> </body> </html>
- templates/confirm.html
<html> <body> <table border = 1> {% for key, value in result.iteritems() %} <tr> <th> {{ key }} </th> <td> {{ value }} </td> </tr> {% endfor %} </table> </body> </html>