In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what is the use of routing Route in Flask. It is very detailed and has a certain reference value. Interested friends must finish reading it!
1. Routing
The so-called routing is the program that handles the relationship between the request url and the function. Different paths of a Web application will have different processing functions. When we request the application, the route will find the corresponding processing function according to the requested url.
2. View function binds multiple url
A view function can bind multiple url. For example, the following code binds / hi and / hello to the hello () function, which registers two routes for the hello () function, which is triggered by users accessing both url.
Based on the previous Hello Flask, add the following functions and run the program.
@ app.route ('/ hi') @ app.route ('/ hello') def hello (): return 'Hello flashover 3, dynamic url
Flask supports adding a variable part to url, which is expressed in the form of < variable name >. Flask will pass the variable to the view function when processing the request, so you can get the value of the variable in the attempt function.
@ app.route ('/ user/') def hello_user (name): return 'Hello {}!' .format (name)
When we access the http://127.0.0.1:5000/hello/tigeriaf address in the browser, we will see "Hello tigeriaf!" on the page. The parameters that follow / hello/ in the url path are received and used by the name parameter of the hello () function.
We can also add a converter before the url parameter to convert the parameter type, such as:
@ app.route ('/ user/') def hello_user (user_id): return 'Hello user: {}!' .format (user_id)
Visit http://127.0.0.1:5000/hello/111 and "Hello user:111!" appears on the page. Among them, parameter type converter int: controls that the type of incoming parameter can only be shaping, and passing other types will report an error of 404.
Currently supported parameter type converters are:
String: character type, but cannot contain slash "/"
Int: integer
Float: floating point type
Uuid:uuid character type
Path: character type, which can contain a slash "/", such as aa/bb/cc
In addition, you can also set the default value of the url variable parameter, as follows, use the defaults parameter setting in the app.route () decorator to receive a dictionary to store the default value mapping of the url variable parameter.
@ app.route ('/ user', defaults= {'name':' default_name'}) @ app.route ('/ user/') def hello_user (name): return 'Hello {}!' .format (name)
In the above code, / user takes no parameters, and when / user is accessed, the variable name uses the default value of "default_name". In fact, this is equivalent to setting the default value for the name variable in the hello_user () function.
4. HTTP request method setting
The commonly used HTTP request methods are GET, POST, PUT, and DELETE. Flask routing can also set the request method, using the methods parameter in the app.route () decorator to pass in an iterable object that contains the listening HTTP request. For example, the following view
The 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'
When using GET request and POST request to access http://127.0.0.1:5000/login, different content will be returned, and if other request methods (such as PUT) are used, 405 Method Not Allowed error will be reported.
5. Url construction
Flask provides the url_for () method to quickly get and build the url. 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.
For example:
@ app.route ('/ superuser') def hello_superuser (): return 'Hello superusername installed app.route (' / user/') def hello_user (name): return 'Hello {}!' .format (name) @ app.route ('/ user/') def hello (name): if name= = 'superuser': return redirect (url_for (' hello_superuser')) else: return redirect (url_for ('hello_user', name=name))
In the above code: the url_for () method gets the url,redirect () based on the attempted function name and redirects to the view function based on the url, both of which are used as the redirection of the url. The hello (name) function accepts the value of the parameter from url, determines whether the value matches the superuser, and if so, redirects the application to the hello_superuser () function using redirect (url_for ()), otherwise to the hello_user () function.
The above is all the content of the article "what is the use of routing Route in Flask". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.