我是Flask的新手,我正在尝试使用python连接本地postgre数据库。
我的代码如下:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@localhost/flaskmovie'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return '<User %r>' % self.username
@app.route('/')
def index():
return "Hello Flask"
if __name__ == "__main__":
app.run()
我得到的错误是:
'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/.
我已阅读文档并尝试了不同的选项,例如:
def create_app():
app = Flask(__name__)
db.init_app(app)
return app
并在python shell中运行以下代码:
app = create_app()
app.app_context().push()
但我仍然得到同样的错误。
任何提示?