Issue
I wanted to create a DB with SQLAlchemey but i keep getting stuck on the same Error and i dont know how to solve it or what i am doing wrong.
My Code and my Terminal
app.py
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODI'] = False
db = SQLAlchemy(app)
class Item(db.Model):
name = db.Column(db.String(100))
@app.route("/")
def home():
return render_template('home.html')
if __name__ == '__main__':
app.run()
db.create_all()
Solution
The error shows that you are working outside of application context.
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run()
This code will access db
within the application context. See documentation for app context.
Update: This applies to the first error that you have encountered. See Detlef's answer for the second error.
Answered By - Fire
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.