PRGパターンを利用する
- POST:POSTでフォームからデータを送信
- Redirect : REDIRECTでデータを引き継いでリダイレクトさせる
- GET:GETでリダイレクトで引き継がれたデータを受け取る
app.py
from flask import Flask, redirect, render_template, request, url_for
app = Flask(__name__)
@app.route('/')
def index():
return render_template('input.html', title='Input section')
@app.route('/redirecting', methods=['POST'])
def redirecting():
user_name = request.form['uid']
user_email = request.form['uemail']
return redirect(url_for('output', user_id=user_name, user_email=user_email))
@app.route('/output', methods=['GET'])
def output():
user_id = request.args.get('user_id')
user_email = request.args.get('user_email')
return render_template('output.html', user_id=user_id, user_email=user_email)
if __name__ == ('__main__'):
app.run(debug=True)
input.html
<body>
<h1>Input</h1>
<form method="POST" action="{{ url_for('redirecting') }}">
<p><strong>User ID : </strong><input type="text" name="uid" size="40"> </p>
<p><strong>User Email : </strong><input type="text" name="uemail" size="40"> </p>
<button type="submit"> Submit </button>
</form>
</body>
output.html
<body>
<h1>Output</h1>
<p>Please check the following information</p>
<p><strong>User ID : </strong>{{ user_id }}</p>
<p><strong>User Email : </strong>{{ user_email }}</p>
<input type="button" onclick="location.href='{{ url_for('index')}}'" value="Back">
</body>