I have built a flask web application using some tutorial on youtube. Its was working fine till I tried to add new tables to the db. I created the table class in models.py. But when I try to run db.create_all()
from the python console after running from myapp import db
, I get this error:
RuntimeError: No application found. Either work inside a view function or push an application context.
I have checked the similar questions but I couldn't find a clear answer. Everyone expects some level of proficiency with flask which apparently I don't have yet. That's the reason I am seeking help here. I think the problem is that imported db has no idea about models. I don't know how to let it know about classes in models. Please help!
This is how myapp/init.py file looks:
#IMPORTING NECESSARY LIBRARIES TO CREATE USEFUL OBJECTS
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from myapp.config import Config
# CREATING USEFUL OBJECTS FOR USAGE IN OUR APP
mail = Mail()
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
def create_app(config_class=Config):
# creating app variable out of Flask class
app = Flask(__name__)
app.config.from_object(Config)
# CONNECTING USEFUL OBJECTS WITH FLASK OBJECT APP
mail.init_app(app)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
# ADDING ROUTES TO THE FLASK OBJECT APP
from myapp.users.routes import users
from myapp.posts.routes import posts
from myapp.main.routes import main
from myapp.errors.handlers import errors
from myapp.finance.routes import finance_data
app.register_blueprint(users)
app.register_blueprint(posts)
app.register_blueprint(main)
app.register_blueprint(errors)
app.register_blueprint(finance_data)
return app
This is how myapp/models.py file looks:
from myapp import db, login_manager
from datetime import datetime
from flask_login import UserMixin
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
import time
today = datetime.today()
datem = datetime(today.year, today.month, 1)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post',backref='author', lazy=True)
fin = db.relationship('Finance', backref='author', lazy=True)
def __repr__(self):
return f"User('{self.username}','{self.email}','{self.image_file}')"
def get_reset_token(self, expires_sec=1800):
s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
return s.dumps({'user_id':self.id}).decode('utf-8')
@staticmethod
def verify_reset_token(token):
s = Serializer(current_app.config['SECRET_KEY'])
try:
user_id = s.loads(token)['user_id']
except:
return None
return User.query.get(user_id)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.title}','{self.date_posted}')"
class Finance(db.Model):
id = db.Column(db.Integer, primary_key=True)
month = db.Column(db.DateTime, nullable=False, default=datem)
revenue = db.Column(db.Float, nullable=False,default=0)
Other_income = db.Column(db.Float,nullable=False,default=0)
total_income = db.Column(db.Float, nullable=False,default=0)
cogs = db.Column(db.Float,nullable=False,default=0)
gross_profit = db.Column(db.Float, nullable=False,default=0)
sga_expenses = db.Column(db.Float,nullable=False,default=0)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Finance('{self.month}','{self.user_id}')"
This is how the run.py file looks:
from myapp import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)