This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.
In the previous Flask Introduction series, Hello Flask! In Flask, we write a Hello Flask application using the Flask framework. We know that the Flask framework is simple and efficient, and can be used quickly. Next, we will introduce the functions of the Flask framework in detail.
routing
The so-called routing, is to deal with the relationship between the request URL and the function of the program, a Web application path will have different processing functions, when we request the application, the routing will find the corresponding processing function according to the URL request.
View functions bind multiple urls
A view function can bind multiple urls. For example, the following code binds both /hi and /hello to the hello() function. This registers two routes for the Hello () function, which is triggered when the user accesses both urls.
Build on the previous Hello Flask article by adding the following functions and running the program.
@app.route('/hi')
@app.route('/hello')
def hello() :
return 'Hello Flask! '
Copy the code
Dynamic urls
Flask supports adding a variable part to the URL in the form of < variable name >, which Flask passes into view functions when processing requests, so you can retrieve the value of the variable in the attempt function.
@app.route('/user/<name>')
def hello_user(name) :
return 'Hello {}! '.format(name)
Copy the code
When we visit http://127.0.0.1:5000/hello/tigeriaf in the browser address, the page will show “Hello tigeriaf!” . The argument after /hello/ in the url path is received and used by the name argument of the hello() function.
We can also add a converter to the URL parameter to convert the parameter type, for example:
@app.route('/user/<int:user_id>')
def hello_user(user_id) :
return 'Hello user:{}! '.format(user_id)
Copy the code
Visit http://127.0.0.1:5000/hello/111, the page will display “Hello user: 111!” . Int: controls the type of the input parameter to be an integer. If the input parameter is of any other type, a 404 error will be reported.
string
: character, but cannot contain slash “/”int
Type:float
: floating-pointuuid
: indicates the uUID character typepath
: a character that can contain a slash (/), such as aa, bb, or cc
You can also set default values for URL variable parameters, as shown in the appl.route () decorator, using the defaults parameter Settings to receive a dictionary to store default mapping of URL variable parameters.
@app.route('/user', defaults={'name': 'default_name'})
@app.route('/user/<name>')
def hello_user(name) :
return 'Hello {}! '.format(name)
Copy the code
In the above code, /user takes no arguments, and when /user is accessed, the variable name uses the default value “default_name”. In fact, this is equivalent to setting the default value of the name variable within the hello_user() function.
HTTP request method Settings
Common HTTP request methods include GET, POST, PUT, and DELETE. Flask routing can also set request methods, passing in an iterable containing the listening HTTP request using the methods argument in the app.route() decorator. For example, the following view function listens for both GET and POST requests:
from flask import request
@app.route('/login', methods=['GET'.'POST'])
def login() :
if request.method == 'POST':
return 'This is a POST request'
else:
return 'This is a GET request'
Copy the code
Respectively using the GET and POST request to http://127.0.0.1:5000/login, will return to different content, if use other request Method (PUT), will to 405 Method Not Allowed error.
Url building
Flask provides the url_for() method to get and build urls quickly. The first parameter of the method is the name of the view function, followed by one or more parameters corresponding to the URL variable part. Such as:
@app.route('/superuser')
def hello_superuser() :
return 'Hello superuser! '
@app.route('/user/<name>')
def hello_user(name) :
return 'Hello {}! '.format(name)
@app.route('/user/<name>')
def hello(name) :
if name == 'superuser':
return redirect(url_for('hello_superuser'))
else:
return redirect(url_for('hello_user', name=name))
Copy the code
In this code, the url_for() method gets the URL based on the name of the attempt function, and the redirect() method redirects the view function based on the URL. The hello(name) function takes the value of the parameter from the URL, determines whether the value matches the superuser, and if so, redirects the application to the hello_superuser() function using redirect(url_for()), Otherwise redirect to the hello_user() function.
Original is not easy, if small partners feel helpful, please click a “like” and then go ~
Finally, thank my girlfriend for her tolerance, understanding and support in work and life!