Check if flack request context is accessible

I want to register some data from context variables ( request , session ) during registration during a flash drive request, but use the default behavior if not.

I am using a try ... except block in logging.formatter . Is there a better way to check the request context?

 try: record.user = session['user_name'] record.very_important_data = request.super_secret except Exception: record.user = None 
+7
python flask logging werkzeug
source share
1 answer

Use has_request_context or has_app_context .

 if has_request_context(): # request is active 

Alternatively, current_app , g , request and session are all instances of LocalProxy . If the proxy server is not connected, it will be False processed as logical.

 if request: # request is active 
+11
source share

All Articles