――If you just make a form, you don't have to mess with anything on the py side (is there such a thing?)
html
<form action="/result" method="post">
<label for="article">Post</label>
<input type="text" name="article">
<p></p>
<label for="name">name</label>
<input type="text" name="name">
<button type="submit">Send</button>
</form>
--ʻAction = "/ result" specifies to send data to / result --Determine what method to use with
method =" post " --ʻInput type = "text" name = "article"
decides to make a one-line input field with text
, and colors the data input there as article.
――The bottom is almost the same
--- By specifying <button type =" submit "> submit </ button>
as submit
, a button to submit data is created.
--This <form>
is used when creating an input / submit form.
--First, preparation on the python side to receive
python
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route("/")
def show():
message = "Hello World"
return render_template("form.html", message = message)
@app.route("/result", methods=["POST"])
def result():
article = request.form["article"]
name = request.form["name"]
return render_template("form.html",article = article, name = name)
--Declare above using request
--Write that the POST method is used by specifying the address specified on the html side above with @ app.route ("/ result", methods = ["POST"])
.
--The place where the variable was done is request.form ["~~~ "]
, so that you can receive the data of the name attribute attached on the html side
Here, you are receiving ʻarticle` etc.
――The rest is almost as you see it
html
<form action="/result" method="post">
<label for="article">Post</label>
<input type="text" name="article">
<p></p>
<label for="name">name</label>
<input type="text" name="name">
<button type="submit">Send</button>
</form>
<p>{{ article }} {{ name }}</p>
--It's almost the same as the above html, but {{article}} {{name}}
Only here is different
Well, it's the same as displaying variables
--If you use the GET method, when you receive the input data and display / result
, it will look like / result? Article = Hello & name = World
, and the input contents will appear in the address.
python
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route("/")
def show():
message = "Hello World"
return render_template("form.html", message = message)
@app.route("/result", methods=["GET", "POST"])
def result():
if request.method == "POST":
article = request.form["article"]
name = request.form["name"]
else:
article = request.args.get("article")
name = request.args.get("name")
return render_template("form.html", article = article, name = name)
--methods = ["GET "," POST "]
makes both methods compatible
--ʻIf request.method == When the method requested by "POST": is POST ... --GET is written as
request.args.get ("~~~") like ʻarticle = request.args.get ("article ")
--For html, it is OK if you set the method written to method =" get "
――For the time being, like this ――I feel that the future is getting longer
Recommended Posts