Try to make a WEB application for phone book with flask Part 1 (Connection with SQLITE, displayed in Flask) Try to make a WEB application for phone book with flask Part 2 (How to handle POST and GET in Flask) Try to make a WEB application for phone book with flask Part 3 (Add registration form) Try to make a web application for phone book with flask Part 4 (Add search form)
Since I was able to implement the display part of the WEB application for the phone book, I thought about creating the registration part next, but I stumbled before that, so I will write it here for organization.
It seems that GET is displayed in the part displayed by Alt + D, and POST is to receive data in an invisible state other than that. This time I will try the method of receiving by POST.
Tutorial was easy to understand.
I chewed the jammed part and made it like this.
First, create an input screen for POST.
post.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset = "utf-8">
</head>
<body>
<form action = "{{url_for("add_ent")}}" method = "POST">
<input name = "Name" placeholder="name">
<input type ="submit" value = "Send">
</form>
</body>
</html>
【point】 You can choose the function to execute with {{url_for ("function in app"}}. Here is the instruction to execute the add_ent function in POST.
Next, write the minimum app.
formtest.py
# -*- coding:utf-8 -*_
from flask import Flask,request,render_template,redirect,url_for
app = Flask(__name__)
@app.route("/")
def hello():
return render_template("form_test.html")
@app.route("/add_entry" ,methods = ["POST"])
def add_ent():
try:
print(request.form["Name"])
finally:
return redirect(url_for("hello"))
if __name__ == '__main__':
app.run(debug = True)
【point】 Please execute the add_ent function in HTML, so define the add_ent function. When you execute the add_ent function from the HTML side, you will be automatically redirected to localhost: 5000 / add. It seems that routing to the add directory is needed to receive the POST method.
Run formtest.py and
If the text sent to the terminal is displayed, it is successful.
I finally understood this. There are too many spells.
Recommended Posts