Flask: Request-response _flask

Source: Internet
Author: User
Request scheduling

When the program receives a request from the client, the view function to process the request is found. To complete this task, flask finds the requested URL in the URL map of the program. A URL mapping is a correspondence between a URL and a view function. Flask generates mappings using the App.route adorner or the App.add_url_rule () in the form of a non adorner.

You can use App.url_map to view URL mappings.

>>> from Hello Import app
>>> app.url_map
map ([<rule '/"(GET, Head, OPTIONS)-> Index> ;,
 <rule '/static/<filename> ' (GET, head, OPTIONS)-> static>
 <rule '/user/<name> ' ( Get, head, OPTIONS)-> User>]

/And/user/<name> routing is the use of the App.route adorner definition in a program. /static/<filename> routing is a special route added by flask to access static files.

The head, Options, and get are the request methods in the URL mapping and are processed by the route. FLASK Specifies the request method for each route, and when different request methods are sent to the same URL, different view functions are used for processing. Head and Options methods are automatically processed by flask. Request Hooks

Sometimes it can be useful to execute code before or after a request is processed. For example, at the start of a request, we might need to create a database connection or authenticate the user who initiated the request. To avoid the use of duplicate code in each view function, Flask provides the ability to register common functions, which can be invoked before or after a request is distributed to a view function.

The request hook is implemented using an adorner, and flask supports the following 4 hook before_first_request: Registers a function that runs before the first request is processed. Before_request: Registers a function that runs before each request. After_request: Registers a function that runs after each request if there are no unhandled exceptions to throw. Teardown_request: Registers a function, even if an unhandled exception is thrown, and runs after each request.

Sharing data between the request hook function and the view function generally uses context global variable G. For example, the Before_request handler can load the logged-on user from the database and save it to G.user. When the view function is called later, the view function then uses G.user to get the user. Response

Flask The View function is invoked, its return value is used as the content of the response. In most cases, the response is a simple string that serves as an HTML page loopback client.

However, the HTTP protocol also needs to return a status code, flask by default to 200, which indicates that the request has been successfully processed. If the response returned by the view function requires a different status code, you can add the numeric code as a second return value after the response text:

@app. Route ('/')
def index (): Return
    ' 

The Flask view function can also return response objects. The Make_response () function can receive 1, 2, or 3 parameters and return a response object. The following example creates a response object and then sets the cookie:

From flask import Make_response

@app. Route ('/')
def index ():
    response = make_response (' 

There is also a special type of response: redirection. This response does not have a page document and only tells the browser a new address to load the new page.

Flask provides a redirect () helper function to generate this response:

From flask Import redirect

@app. Route ('/")
def Index (): Return
    redirect (' http://www.example.com ')

There is also a special response to handle the error, which is generated by the Abort function, and in the following example returns status code 404 if the user of the dynamic parameter ID in the URL does not exist:

From flask import Abort

@app. Route ('/user/<id> ')
def get_user (ID):
    user = Load_user (ID)
    IF Not User:
        abort (404) return
    ' 

Instead of returning control to the function that invoked it, abort throws the control over to the Web server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.