Почему не создается база данных на основе sqlite Flask ? Помогите пожалуйста
Проблема в том, что база данных не создаётся, я набираю в терминале IDE:
from Main import db.create_all()
После чего выдается такая ошибка:
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.
Сам код:
from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.bd"
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), nullable=False)
intro = db.Column(db.String(150), nullable=False)
text = db.Column(db.Text, nullable=False)
date = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<User %r>' % self.id
@app.route('/')
@app.route('/home')
def main():
return render_template("index.html")
@app.route('/about')
def abouts_page():
return render_template("about.html")
if __name__=='__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
Источник: Stack Overflow на русском