Application context and request context

Source: Internet
Author: User

 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

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.