0

I keep getting the below error regarding the RuntimeError: Working outside of application context. when i use db.create_all() on terminal Please could someone point out where i'm going wrong.

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.db'
db = SQLAlchemy(app)
class Todo(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    content = db.Column(db.String(200),nullable=False)
    date_created = db.Column(db.DateTime,default=datetime.utcnow)

    def __repr__(self):
        return '<Task %r>' % self.id

@app.route('/')
def index():
    return render_template('index.html')

if __name__ =="__main__":
    app.run(debug=True)

Error : 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.

0

2 Answers 2

0

or you can do so

@app.route('/create')
def create():
    db.create_all()
    return 'All tables created'

it should be in your main.py then you navigate to your browser localhost:port_number/create

Sign up to request clarification or add additional context in comments.

Comments

0

Use flask shell in your terminal instead of python to get to a Python console. This provides the Flask application context you need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.