从主应用程序导入烧瓶蓝图

从主应用程序导入烧瓶蓝图

本文介绍了从主应用程序导入烧瓶蓝图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写带有一个蓝图的应用程序.我的应用程序使用Flask-SQLAlchemy,因此我的蓝图需要访问主应用程序的 db 对象(由Flask-SQLAlchemy创建),以便创建自己的模型.

I'm writing an application with one blueprint. My application uses Flask-SQLAlchemy, so my blueprint needs access to the main app's db object (created by Flask-SQLAlchemy) in order to create its own models.

但是,当我尝试使用 current_app.db 获取 db 对象时,flask给了我以下错误:

However, when I try to get the db object with current_app.db, flask gives me the following error:

RuntimeError: working outside of application context

这是我的主要 __ init __.py :

from flask import Flask

from app.uploader import uploader

app = Flask(__name__)

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)

app.register_blueprint(uploader)

这是我的 uploader 蓝图中的 __ init __.py :

from flask import Blueprint

uploader = Blueprint('uploader', __name__,
    template_folder='templates')

from . import views
from .models import *

这里是发生异常的蓝图的 views.py :

Here's views.py of the blueprint, where the exception takes place:

from flask import (redirect, render_template, request, send_from_directory,
    session, current_app)
from flask.views import View
from werkzeug import secure_filename

print current_app.db # Exception happens here

这是堆栈跟踪:

Traceback (most recent call last):
  File "runtests.py", line 11, in <module>
    import tests
  File "/home/plasmasheep/project/tests.py", line 14, in <module>
    from app import app, db, user_datastore
  File "/home/plasmasheep/project/app/__init__.py", line 6, in <module>
    from app.uploader import uploader
  File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
    from . import views
  File "/home/plasmasheep/project/app/uploader/views.py", line 18, in <module>
    print current_app.db
  File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

仅尝试从.. import db 使用不起作用:

Simply trying to use from .. import db does not work:

Traceback (most recent call last):
  File "runtests.py", line 11, in <module>
    import tests
  File "/home/plasmasheep/project/tests.py", line 14, in <module>
    from app import app, db, user_datastore
  File "/home/plasmasheep/project/app/__init__.py", line 7, in <module>
    from app.uploader import uploader
  File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
    from . import views
  File "/home/plasmasheep/project/app/uploader/views.py", line 17, in <module>
    from .. import db
ImportError: cannot import name db

也没有从应用程序导入数据库中 :

Nor does from app import db:

Traceback (most recent call last):
  File "runtests.py", line 11, in <module>
    import tests
  File "/home/plasmasheep/project/tests.py", line 14, in <module>
    from app import app, db, user_datastore
  File "/home/plasmasheep/project/app/__init__.py", line 7, in <module>
    from app.uploader import uploader
  File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
    from . import views
  File "/home/plasmasheep/project/app/uploader/views.py", line 17, in <module>
    from app import db
ImportError: cannot import name db

推荐答案

current_app 仅在(基本上)请求/响应周期内设置.通常,仅在内部视图或保证被称为内部视图的内容中使用此功能.当您无权直接访问应用程序时(例如,如果您正在使用应用程序工厂),通常会使用 current_app .由于您没有使用工厂,因此只需直接导入db,它就可以在您的情况下正常工作.

current_app is only set during (essentially) a request/response cycle. Normally, you use this only inside views, or stuff that is guaranteed to be called inside views. You typically use current_app when you don't have access to the app directly, such as if you are using an application factory. Since you're not using a factory, just import db directly and it should work in your case.

导入错误是由于循环导入引起的.将行从app.uploader导入上传器移到 db 的定义之后.请参阅本部分中的几段,其中提到了导入定义它们的任何依赖关系后的视图.

The import error is due to a circular import. Move the line from app.uploader import uploader to after the definition of db. See a couple paragraphs into this section of the docs, which mentions importing views after defining any of their dependencies.

这篇关于从主应用程序导入烧瓶蓝图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 16:03