(For myself) Flask_3 (form, POST and GET)

item

  1. What is an input form and a light form?
  2. A bulletin board-like guy with the POST method
  3. A bulletin board-like guy with the GET method

1. Feel the form in the input form

――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.

2. Receive the input data using the POST method

--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

3. Receive input data using GET method

--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 "

4. At the end

――For the time being, like this ――I feel that the future is getting longer

Recommended Posts

(For myself) Flask_3 (form, POST and GET)
Get query parameters for Flask GET
(For myself) Flask_2 (list and for, extends, and a little more)
(For myself) Flask_7 (Open database from Flask)
(For myself) Flask_ex (templates and static)
(For myself) Flask_5 (Add to txt file)
POST variously with Python and receive with Flask
(For myself) Put Flask in VS Code
Get parameter and Post body, enable CORS in Flask (Python) and Express (Node.js)
python [for myself]
POST the image with json and receive it with flask
* Android * [HTTP communication_2] HTTP communication with Flask (hit WebAPI [GET, POST])
GET / POST communication by Flask (also about CORS support)
Flask Tutorial # 1 ~ GET Request ~
heroku memo (for myself)
Freecad memorandum (for myself)
Compare HTTP GET / POST with cURL (command) and Python (programming)
(For myself) Flask_6 (Open db from python, Mysql basic (phpMyAdmin))
Search for variables in pandas.DataFrame and get the corresponding row.
(For myself) Flask_4 (Drop-down menu, view txt data (using table))