1. When I was practicing Flask Web development, models.py could not import app, from app import db, import db, and from . import db. Neither of them worked. Can I not quote it like this?
2. The code structure is as follows:
I want to imprt the app's db in models.py in the app, but it has been unsuccessful. When I use from . import db, the error is Attempted relative import in non-package , when using from app import db, the error is cannot import name db
__init__ code is as follows:
from flask import Flask,render_template
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
bootstrap=Bootstrap()
mail=Mail()
moment=Moment()
db=SQLAlchemy()
login_manager=LoginManager
def create_app(config_name):
app=Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from .main import main as blueprint
app.register_blueprint(blueprint)
return app
Put models under the main folder.