I'm sorry to say that there is a memorial element.
Write a template-like program below. The directory structure is
Qiita/ ├templates │ ├index.html │ └posted.html └app.py
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
##Up to this point is like a template
@app.route('/home')
def home():
return render_template('index.html')
@app.route('/post',methods=['POST','GET'])
def post():
if request.method == 'POST':
all = request.form
return render_template('check.html',name=name,val = val)
if __name__ == "__main__":
#You can also set the port to open here.
app.run(host='0.0.0.0',port=5000,debug=True)
Now I would like to explain this program.
python
@app.route('/home')
Is this checking if you accessed http://0.0.0.0:5000/home
? It is a thing.
After this
python
return render_template('index.html')
Then run templates / index.html
.
python
@app.route('/post',methods=['POST','GET'])
def post():
if request.method == 'POST':
all = request.form
return render_template('posted.html',data=all)
Then, if it is ** POST ** or ** GET ** method, it will be processed.
Also, request.form
receives POSTed data in dict
type. For example, if name =" tsukkey "
is entered, enter request.form ['name']
to refer to tsukkey.
With data = all
inrender_template ()
, it feels like sending the POSTed value to posted.html.
As for the html file, I will not raise it because it is troublesome, but if you write an if statement or something by enclosing it in {%%}
, it is basically quite good.
Recommended Posts