Python3 Flask framework study notes (a)

Summary: Flask web is a micro frame. "Micro" does not mean Flask function is not strong. Micro framework of the "micro" word indicates Flask goal is to keep the core simple and scalable.

A, Flask installation

pip install flask

Second, create a project Flask

  • First, we imported the Flask class.
  • Examples of the class created app, the first parameter is the name of the application or the module package. If you use a single module, you should use _ name _.
  • Using the route () decorator to bind URL function.
    Example:
from flask import Flask
from flask import request

app = Flask(__name__)


@app.route('/study', methods=['GET'])
def hello_world():
    return 'Hello World!'


@app.route('/study/<username>', methods=['GET'])
def studyvariable(username):
    print(request.method)
    print(request.url)
    return 'Hello, {}'.format(username)

@app.route('/study/studypost', methods=['POST'])
def study_post():
    data = request.form
    print(type(data))
    print(data)
    print(data['username'])
    return 'ok'


if __name__ == '__main__':
    app.run()

Third, the correlation method, the object

(1)app.route(rule, options)

  • rule parameter indicates the URL binding and function.
  • options is a list of parameters to be forwarded to the underlying Rule objects.
  • methods received is a list: GET, POST etc.

(2) app.run (Host, Port, Debug, Options) (All parameters are optional)

  • host: To monitor the host name, the default is 127.0.0.1
  • port: the port number is provided, the default value is 5,000
  • debug: Debugging is turned on, the default is false . If set to true, the debug information provided
  • options: to be forwarded to the underlying Werkzeug server.

(3)request

  • In Flask global objects by providing request information to the request, the operation request data

(4) variable rules
by the URL is part of the tag <variable_name> can add variables in the URL. Labeled moiety as keyword arguments passed to the function. By using the <converter: variable_name>, you can selectively add a converter, a variable designated rule. Variables such as the example "<username>"

  • Converter type:
Types of description
string (Default value) accept any text that does not contain a slash
int Receiving a positive integer
float Receiving positive float
path Similar string, but it may contain slashes
uuid Accept the UUID string

Fourth, debugging project

  • Console output after starting the project:
FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder F:/Python项目/untitled
F:\Python项目\untitled\venv\Scripts\python.exe -m flask run
 * Serving Flask app "app.py"
 * Environment: development
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  • By Postman debugging

Sending a Get request: http: //127.0.0.1: 5000 / study / wangwu
response: Hello, wangwu

Console output:

127.0.0.1 - - [25/Feb/2020 14:13:15] "GET /study/wangwu HTTP/1.1" 200 -
GET
http://127.0.0.1:5000/study/wangwu

Post send request: http: //127.0.0.1: 5000 / study / studypost
form data: { 'username': 'lisi ', 'password': '123456'}
Response: ok

Console output:

<class 'werkzeug.datastructures.ImmutableMultiDict'>
ImmutableMultiDict([('username', 'lisi'), ('password', '123456')])
lisi
127.0.0.1 - - [25/Feb/2020 14:26:42] "POST /study/studypost HTTP/1.1" 200 -
Published 40 original articles · won praise 31 · views 620 000 +

Guess you like

Origin blog.csdn.net/weixin_38422258/article/details/104495679