[PYTHON] Let's make a WEB application for phone book with flask Part 2

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)

Until last time

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.

First of all, about POST and GET

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.

How to receive POST in Flask

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 2017-05-12_103736.jpg

2017-05-12_103800.jpg

If the text sent to the terminal is displayed, it is successful.

2017-05-12_103845.jpg

I finally understood this. There are too many spells.

Recommended Posts

Let's make a WEB application for phone book with flask Part 1
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Let's make an A to B conversion web application with Flask! From scratch ...
Let's make a web framework with Python! (1)
Let's make a web framework with Python! (2)
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 3] ~ Implementation of nervous breakdown ~
Web application development with Flask
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 6] ~ User Authentication 2 ~
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 5] ~ User authentication ~
Web application with Python + Flask ② ③
Web application with Python + Flask ④
I made a simple book application with python + Flask ~ Introduction ~
Creating a web application using Flask ②
Let's make a GUI with python.
Let's make a breakout with wxPython
Build a web application with Django
Creating a web application using Flask ①
Make Flask a Cloud Native application
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Creating a web application using Flask ③
Creating a web application using Flask ④
If you know Python, you can make a web application with Django
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Let's make a web chat using WebSocket with AWS serverless (Python)!
Let's make a shiritori game with Python
Make a rare gacha simulator with Flask
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
[Python] A quick web application with Bottle!
Create a simple web app with flask
Run a Python web application with Docker
Create a web service with Docker + Flask
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a Twitter Bot with Python!
Let's make a Backend plugin for Errbot
I made a WEB application with Django
[Streamlit] I hate JavaScript, so I make a web application only with Python
Flask + PyPy I took a speed benchmark with Blueprint for large-scale Web
I want to make a web application using React and Python flask
Publish a web application for viewing data created with Streamlit on heroku
Make a simple pixel art generator with Flask
Launch a web server with Python and Flask
Let's replace UWSC with Python (5) Let's make a Robot
Let's make a module for Python using SWIG
Data acquisition from analytics API with Google API Client for python Part 2 Web application
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Web application production course learned with Flask of Python Part 2 Chapter 1 ~ JSON exchange ~
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 2] ~ Vue setup ~
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 1] ~ Django setup ~
Let's make an app that can search similar images with Python and Flask Part2
Make a morphological analysis bot loosely with LINE + Flask
How to make a shooting game with toio (Part 1)
Let's make a simple game with Python 3 and iPhone
Parse and visualize JSON (Web application ⑤ with Python + Flask)
[Part 2] Let's build a web server on EC2 Linux
Launch a Python web application with Nginx + Gunicorn with Docker
Web application made with Python3.4 + Django (Part.1 Environment construction)