flask之current_app的使用及详解

【current_app】
之前在写项目的时候也有用到current_app,大牛讲的是代表了当前项目的app,当然写完项目也没有任何问题。

from flask import Flask, current_app

app = Flask(__name__)
print(app)    #输出结果:<Flask '1_current_app'>

相信大家对这段代码是没有问题的,也不知道有啥作用,那么请看下面这段代码

from flask import Flask, current_app

app = Flask(__name__)
print(app)    #输出结果:<Flask '1_current_app'>
app2 = current_app
print(app2)    #输出结果:竟然报了一大堆的错误

错误信息是这样子的:RuntimeError: Working outside of application context.意思是说我们在应用上下文之外运行的,这究竟是咋回事,请看下面这幅图


原来在flask内部维护者两个线程隔离的栈,current_app指向了AppContext(应用上下文)中的栈顶,request指向了RequestContext(请求上下文)栈顶,当请求进入的时候,Request对象被压入栈,从而request有了指向处理请求,接下来会判断AppContext栈顶是否为空,若为空则向栈中压入一个AppContext对象,即app,从而current_app就有了指向,所以我们在项目中使用是没有报错的,而我们上面的代码不是在请求中实现的所以AppContext栈顶为空,current_app并没有指向一个AppContext对象,怎样解决呢?

上面不如下面代码美观

from flask import Flask, current_app

app = Flask(__name__)
print(app)    #输出结果:<Flask '1_current_app'>
with app.app_context():
    app2 = current_app
    print(app2)    #输出结果:<Flask '1_current_app'>

这里我们使用了with,其app_context()返回一个AppContext对象,而其又实现了__enter__与__exit__分别让AppContext对象,即app入栈和出栈,完成了此操作。

 

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
This error message indicates that there is an issue with importing the `current_app` object from the `flask` module. This object is used to access the current Flask application instance, and is typically used in a context where the application instance is not available through other means. There are a few possible reasons why you might encounter this error: 1. You may be importing `current_app` from the wrong module. Make sure that you are importing it from the `flask` module and not another module with a similar name. 2. You may be importing `current_app` before the Flask application has been created. `current_app` is only available when a Flask application context is active, so if you try to import it outside of a request or application context, you will get this error. Make sure that you are importing `current_app` from within a Flask view function, or from within a function that is called within a Flask view function. 3. There may be a circular import issue in your application. Flask applications can be prone to circular import issues if you're not careful about how you structure your code. If you're importing `current_app` from a module that also imports something from the module where your Flask application instance is created, you may run into this error. Try restructuring your code to avoid circular imports. To resolve this error, you should check your code for the above issues and make sure that you are importing `current_app` correctly and in the right context. You may also want to review the Flask documentation on application context and request context to better understand how they work.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值