Getting started with [email protected]() use


1. What is @app.route()?

  • In Python, as long as it is carried @, it is basically a decorator. The essence of a decorator is a function that extends the function of the original function. For details, see another article "Python Custom Decorator"
  • And here app.route('URL')is a very important decorator in the Flask framework. Its function is to decorate a view function when the program is running, and register it with the given URL rules and options. It doesn’t matter if you don’t understand it here, just use it .

2. Basic understanding

Example 1:
insert image description here

1. First we import the Flask class. Instances of this class will be our WSGI application (related to route registration).
2. Then we create an instance of this class. The first parameter is the name of the application module or package. name is a shortcut that works in most cases. With this parameter, Flask knows where to find things like templates and static files.
3. Then we use the route() decorator to tell Flask the URL to trigger the function on.
4. The function returns the information that needs to be displayed in the user's browser. The default content type is HTML, so HTML in the string will be rendered by the browser.

Example 2:
insert image description here

  • From here, we can boldly guess @app.route('URL')that the function is to run the program, and then input here URL, and the value of the function will be displayed on the page return.
  • 建议:Try to keep the same function name as the modified function, so that you can greatly avoid the program error caused by the same function name in the same file route(). URL(There is no essential relationship between the URL and the modified function name here, and the suggestion here is just a coding habit)

Three, URL writing rules

insert image description here
Summarize:

  • In app.route('/TestB/'), there are slashes before and after TestB. When accessing, whether to add a slash after TestB or not, Flask will redirect to (/TestB/), and the access is successful.
  • If there is no slash after TestA in app.route('/TestA`), then when accessing (/TestA/), an error will be reported, and (/TestA) will be accessed successfully.

注:If the URL ends with a non-letter, the above rules will be changed, and only a strictly correct URL can be accessed successfully (don’t ask, it will be bitter tears if you ask too much) write
建议:in @app.route('') ​​with URL addresses with double slashes.

4. @app.route() with parameters

The following variable rules are common:

keywords effect
string Accepts any text that does not contain a slash
int Accepts positive integers
float Accepts positive floating point numbers
path Like string, but can contain slashes
uuid Accepts UUID strings

Example:
insert image description here

注:Here, returnonly character types or dictionaries can be returned, and other tuples, lists, and integers cannot be returned, so the middle one in the above three examples uses forced type str()conversion and string splicing as the return value.

5. Parameter splicing and passing through?

Example:
insert image description here

  • It is used here request, and this dependency needs to importbe added after the statement ;request
  • Generally, parameter passing is used to splicing routes and parameters, and &splicing between multiple parameters.

6. Select route jump

Example:
insert image description here

  • Here ('/any(blog,infor):url_path/<id>/')means that the input urlis blogor is inforused to url_pathreceive, idit is a parameter, and is used to receive data.

app.route()The above are some usages that you may encounter in normal times . You don’t need to deliberately memorize, understand the memory, read other people’s code, and just understand it.

Guess you like

Origin blog.csdn.net/qq_44864833/article/details/122423539