作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 flask-sqlalchemy,并且我已经遵循文档中的大量示例来了解如何设置 Flask 应用程序。但是,出于某种原因,我无法获取应用程序上下文。我目前正在使用 app.app_context().push()
方法。还有其他方法可以使它起作用吗?我究竟做错了什么?任何帮助将不胜感激。
当前运行:
Python==3.6.3
Flask==0.12.2
Flask-Compress==1.4.0
Flask-SQLAlchemy==2.3.2
模型.py
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
db = SQLAlchemy()
class Property(db.Model):
__tablename__ = 'vt_property'
id = db.Column(db.BigInteger, primary_key=True)
name = db.Column(db.String(32))
value = db.Column(db.LargeBinary)
client_tag = db.Column(db.String(16))
索引.py
from flask import Flask, jsonify, request
from flask_compress import Compress
from database.model import *
from manager.some_util import some_user, some_url, some_pass
# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
Compress(app)
app.secret_key = secret_key
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://connection_string'
db.init_app(app)
app.app_context().push()
some_util.py
from database.model import Property
from helpers.util import secret_key
def load_some_config():
from secure.secure import read_encrypted_text
some_configs = Property.query.filter(Property.name.contains('MANAGER')).all()
some_url = ''
some_user = ''
some_pass = ''
for c in some_configs:
if 'URL' in c.name:
some_url = c.value.decode('utf-8')
if 'USER' in c.name:
some_user = c.value.decode('utf-8')
if 'PASS' in c.name:
some_pass = read_encrypted_text(c.value).decode('utf-8')
return some_url, some_user, some_pass
当我尝试运行我的 API 时,出现此错误:
*/venv/bin/python */src/api/index.py
Traceback (most recent call last):
File "*/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 988, in __call__
return self.registry[key]
KeyError: <greenlet.greenlet object at 0xXXXXXXXX>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "*/src/api/index.py", line 16, in <module>
from manager.some_util import some_user, some_url, some_pass
File "*/src/manager/some_util.py", line 23, in <module>
some_url, some_user, some_pass = load_some_config()
File "*/src/manager/some_util.py", line 9, in load_some_config
some_configs = Property.query.filter(Property.name.contains(‘MANAGER’)).all()
File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "*/venv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
return self.registry()
File "*/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 990, in __call__
return self.registry.setdefault(key, self.createfunc())
File "*/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2882, in __call__
return self.class_(**local_kw)
File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 141, in __init__
self.app = app = db.get_app()
File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 912, in get_app
'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
最佳答案
要在 Flask 应用上下文中执行一些代码,最简单的方法是这样做:
with self.app.app_context():
# ... your code here ...
请注意,app
是您的 Flask 应用程序对象。在您的情况下,变量名称可能有所不同。
关于python-3.x - Flask-SQLAlchemy 错误 : 'No application found. Either work inside a view function or push' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014377/
我是一名优秀的程序员,十分优秀!