Python SQLAlchemy应用程序上下文错误
我试图在视图函数中使用SQLAlchemy(我在Flask ApScheduler中做过类似的事情)。 我知道已经有很多与这个主题相关的话题,但是没有一个对我有帮助。Python SQLAlchemy应用程序上下文错误,python,flask,sqlalchemy,flask-sqlalchemy,Python,Flask,Sqlalchemy,Flask Sqlalchemy,我试图在视图函数中使用SQLAlchemy(我在Flask ApScheduler中做过类似的事情)。 我知道已经有很多与这个主题相关的话题,但是没有一个对我有帮助。 首先,我将展示我的代码: /run.py /app/\uuuuu init\uuuuuuuuuuuuuuuuuuy.py /app/models.py /app/node.py /app/bot.py 因此,当我试图从我的应用程序调用cmd\u test时,我遇到以下错误: RuntimeError: Working outsid
首先,我将展示我的代码: /run.py /app/\uuuuu init\uuuuuuuuuuuuuuuuuuy.py /app/models.py /app/node.py /app/bot.py 因此,当我试图从我的应用程序调用
cmd\u test
时,我遇到以下错误:
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
我尝试使用g
变量和before\u request
方法,因为每次调用数据库之前都会调用路由“handler”,但这也不起作用。
我还尝试使用db.get_app(),但没有效果。所以我的问题是如何在视图之外调用数据库?什么是
@bot
?如何运行消息处理程序
?这是一个独立的过程吗?@Danila Ganchar这是一个允许为teleagram创建机器人程序的模块。我正在使用Flask
作为我的机器人的网络钩子。新年快乐!;)telebot
对Flask应用程序一无所知。您不能使用当前的应用程序。尝试使用应用程序导入创建应用程序中的;app=创建_app();使用app.app_context():..
。噢,终于对我起作用了!非常感谢(我很高兴)祝你好运什么是@bot
?如何运行消息处理程序
?这是一个独立的过程吗?@Danila Ganchar这是一个允许为teleagram创建机器人程序的模块。我正在使用Flask
作为我的机器人的网络钩子。新年快乐!;)telebot
对Flask应用程序一无所知。您不能使用当前的应用程序。尝试使用应用程序导入创建应用程序中的;app=创建_app();使用app.app_context():..
。噢,终于对我起作用了!非常感谢(我很高兴)祝你好运
from flask import Flask
from .node import node
from .models import db
def create_app(app_config=None):
app = Flask(__name__, instance_relative_config=False)
app.config.from_object(app_config)
db.init_app(app)
app.register_blueprint(node)
return app
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Users(BaseFuncs, db.Model):
...
from flask import Blueprint, request
from .bot import bot, secret
import telebot
node = Blueprint('node', __name__)
@node.route('/{}'.format(secret), methods=['POST'])
def handler():
bot.process_new_updates([telebot.types.Update.de_json(request.get_data().decode('utf-8'))])
return 'ok', 200
from flask import current_app as app
...
@bot.message_handler(commands=['test'])
def cmd_test(message):
with app.app_context():
print(Users.query.filter_by(id=0).first())
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.