Flask is a lightweight Web application framework written in Python. The WSGI toolkit uses Werkzeug and the template engine uses Jinja2. Flask uses BSD authorization. Flask is also known as a “microframework” because it uses a simple core and adds additional functionality with Extension.
Flask installation: Go to the virtual environment you created and use PIP install XXX to install the Flask in the virtual environment.
Flask: Hello World!
In the command output, 127.0.0.1 indicates that it is run on the local PC. 5000 represents the port number. Click the link to output Hello World! .
Modify the route: Bind the route to /index
Open it to other hosts and add host= “0.0.0.0”
Set the variable to username, and use %s placeholder to output hey flask.
Change the definition variable type to int (float, path, uUID, etc.). Change to int to perform operations on variables.
Flask redirect () : /[Ehualu Developer Community](Developer community (ehualu.com)), which is the official website of developer community.
Install Postman, a popular interface testing tool that can send almost any type of HTTP request. The main page is shown below.
Here you can select the request type, some of which are shown below:
Four basic requests:
1, GET request will be sent to the database to obtain data request, so as to obtain information, the request is like the database select operation, just used to query the data, will not modify, increase the data, will not affect the content of the resource, that is, the request will not produce side effects. No matter how many times you do it, the result is the same. See: GET /url/ XXX
2. Unlike GET, a PUT request sends data to the server to change information. The PUT request is like an update to a database, modifying the content of the data but not increasing the type of data. Update: PUT /url/ XXX
3. A POST request, like a PUT request, sends data to the server, but it changes the type of data and other resources, just like a database insert operation, which creates new content. Almost all current commit operations are POST requests. Create: POST/URL
4. A DELETE request, as its name implies, is used to DELETE a resource, just like a database DELETE operation. DELETE: DELETE /url/ XXX
Use GET request, input the routing address you want to access, send for access, for example, we visit the official website of Yihualu developer community:
POST request:
Output result:
To return the result in JSON format, we need to import Jsonify to achieve the return format of JSON, as shown in the following figure:
When the output is wrong, we can output the reason for the error. Delete the age parameter as shown below, and we can get the feedback of “missing parameter”.
We get an “error” feedback when we write age as STR, which cannot be evaluated.
Session simulates simple login, logout, and checking the login status.
Login: Import the session package and set the session key
Set the fixed values of username and password. If both values are correct, the login succeeds.
If the account or password is incorrect, the system displays incorrect account or password.
Check login status:
Log out:
When we complete login, check login status, feedback to get username.
When we log out, check the login status again.
To this point, we simply simulated the account login, check the login status and log out of the login process.