【发布时间】: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