【问题标题】:RuntimeError: No application found. Either work inside a view function or push an application context. FLASK SQLAlchemy errorRuntimeError:未找到应用程序。在视图函数中工作或推送应用程序上下文。 FLASK SQLAlchemy 错误
【发布时间】:2021-05-05 22:00:20
【问题描述】:

application.py

from flask import Flask, render_template
from wtform_fields import *
from models import *
app = Flask(__name__)
app.secret_key='REPLACE LATER'
app.config['SQLALCHEMY_DATABASE_URI']='*db link*'
db = SQLAlchemy(app)
@app.route("/", methods=['GET', 'POST'])
def index():

    reg_form = RegistrationForm()

    # Update database if validation success
    if reg_form.validate_on_submit():
        username = reg_form.username.data
        password = reg_form.password.data
        user_object = User.query.filter_by(username=username).first()
        if user_object:
         return "Already Taken"
        user =User(username=username,password=password)
        db.session.add(user)
        db.session.commit()
        return "Inserted into DB"
    return render_template("index.html", form=reg_form)
if __name__ == "__main__":
    app.run(debug=True)

models.py

from flask_sqlalchemy import SQLAlchemy 
db = SQLAlchemy()
class User(db.Model):
    """ User model """
    __tablename__="users"
    id=db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(25) , unique=True, nullable=False)
    password = db.Column(db.String(), nullable=False)
    db.create_all()

我收到此错误:

D:\PostgreSQL\12\bin\GSTChat\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1094, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "D:\PostgreSQL\12\bin\GSTChat\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1071, in _execute_for_all_tables
    app = self.get_app(app)
  File "D:\PostgreSQL\12\bin\GSTChat\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1042, in get_app
    raise RuntimeError(
**RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.**

我什至在文档中寻找解决方案,但我无法解决错误。

【问题讨论】:

    标签: python flask runtime-error flask-sqlalchemy


    【解决方案1】:

    编辑:添加另一个答案...

    您可以像在 models.py 文件中一样拥有它,然后在您的应用程序文件中对其进行初始化:

    application.py

    from flask import Flask, render_template
    from wtform_fields import *
    from models import *
    
    app = Flask(__name__)
    # Other code you have
    db.init_app(app)
    

    答案 2:我看到您在 application.py 文件中定义 db,然后在 models.py 文件中再次定义。您应该将它从您的应用程序导入到您的模型文件中。

    models.py

    from flask_sqlalchemy import SQLAlchemy
    from app import db
    
    class User(db.Model):
        """ User model """
        __tablename__="users"
        id=db.Column(db.Integer, primary_key=True)
        username = db.Column(db.String(25) , unique=True, nullable=False)
        password = db.Column(db.String(), nullable=False)
    

    我希望这对您有所帮助并解决您的错误! :) 您可能还想将db.create_all() 放在您的 application.py 文件中(在导入模型之后),而不是放在模型本身中。干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2018-06-09
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2018-08-11
      • 2018-03-14
      相关资源
      最近更新 更多