fromFlaskImportFlask,request,session,url_for,current_app fromWerkzeug.localImportLocal,localstack#Thread Isolation Technology#as long as the properties bound on the local object#is isolated in each threadapp= Flask (__name__)#print (current_app.name) #RuntimeError: Working outside of application context.#How to solve the above errorApp_context =App.app_context () App_context.push ( )#Push the current app to takePrint(Current_app.name)#Flask_context_demo#can be simplified with the WITH statementWith App.app_context ():Print(Current_app.name) @app. Route ('/')defHello_world ():#is to display the name of the current app Print(Current_app.name)Print(Url_for ('my_list')) return 'Hello world!'@app. Route ('/list/')defmy_list ():return 'my list'With App.test_request_context ():#manually pushing a request context into the request context stack #if the current app context stack does not have an app up or down #then you will first push in an application context into the stack Print(Url_for ('my_list')) if __name__=='__main__': App.run ()
Application context and request context