Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Flask Framework Redirection, error display, Responses response and Sessions session Operation

Shulou Source: shulou.com Published: 2022-06-03 19:49:40 11月12日 Update

Editor to share with you the example analysis of Flask framework redirection, error display, Responses response and Sessions session operation. I hope you will get something after reading this article. Let's discuss it together.

The details are as follows:

Redirect and error display

Redirect the user to another endpoint, using redirect (). To abort the error request in advance, use the abort () function

From flask import abort, redirect, url_for@app.route ('/') def index (): return redirect (url_for ('login')) @ app.route (' / login') def login (): abort (401) this_is_never_executed ()

By default, black and white error pages are displayed for each error code. If you want to customize the error page, use the errorhandler () decorator.

Responses

If a response object of the correct type is returned, it is returned directly from the view.

If it is a string, the response object is created using this data and the default parameters.

If a tuple is returned, the items in the tuple can provide additional information. Such a tuple must be in this form, or at least one item must be in the tuple. This value overrides the status code and can be a list or dictionary of other header values. (response, status, headers) or (response, headers)

If you want to get the generated response object in the view, you can use the make_response () function

Suppose you have the following view:

@ app.errorhandler def not_found (error): return render_template ('error.html'), 404

Use make_response () to include the return expression, get the response object and modify it, and then return it

@ app.errorhandler (404) def not_found (error): resp = make_response (render_template ('error.html'), 404) resp.headers [' Xmuring'] ='A value' return resp

Sessions session tracking

Session is implemented on the basis of cookie, and cookie is signed in an encrypted way.

To use sessions, you must set the private key. Here is a simple example:

From flask import Flask, session, redirect, url_for, escape, requestapp = Flask (_ _ name__) # Set the secret key to some random bytes. Keep this really secretsecretapp.secretroomkey = b'_5#y2L "F4Q8z\ n\ xec] /'@ app.route ('/') def index (): if 'username' in session: return' Logged in as% s'% escape (session ['username']) return' You are not logged in'@app.route ('/ login', methods= ['GET') 'POST']) def login (): if request.method =' POST': session ['username'] = request.form [' username'] return redirect (url_for ('index')) return''

'' @ app.route ('/ logout') def logout (): # remove the username from the session if it's there session.pop ('username', None) return redirect (url_for (' index')) after reading this article, I believe you have some understanding of "Flask framework redirection, error display, Responses response and sample analysis of Sessions session operation". If you want to know more about it, please follow the industry information channel. Thank you for reading!

Tags: Errors objects examples views frames analysis code functions articles pages information parameters foundations dictionaries characters strings finished rare forms situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Xiaomi macOS Shulou Information