出典:
from flask import Flask, render_template, request
print(__name__)
app = Flask(__name__)
# reply when access to http://127.0.0.1:5000
@app.route("/")
def hello_world():
return "<p>This is the main page</p>"
#return "<p>Hello, World!</p>"
#"/hoge"に対する処理を記述した場合 /hoge
# reply when access to http://127.0.0.1:5000/hoge
@app.route("/hoge")
def hoge_hoge():
return "<p>hoge, hoge!</p>"
# reply HTMLfile in foleder"templates"
@app.route("/htmlFile")
def html_page():
return render_template('index.html')
@app.route('/form_input')
def form_input():
return render_template('form_input.html')
@app.route('/form_input-post', methods=['POST'])
def form_input_temp():
print('POSTデータ受け取ったので処理します')
req1 = request.form['data1']
return f'POST受け取ったよ: {req1}'
if __name__ == "__main__":
app.run()
#app.run(debug=True)
-----------------------------------------------------------------
以下templatesに入れるHTML
■index.html
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>HTML Sample</title>
</head>
<body>
<div class="main">
<h1>
</h1>
<p>HTMLから読み込んだで</p>
</div>
</body>
</html>
■form_input.html・・・data1に入力値が引き継がれる
<h1>サンプルフォームです。</h1>
<form action="/form_input-post" method="POST">
<input name="data1"></input>
<button type="submit">送信</button>
</form>