In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.