1.我在练习Flask Web开发的时候,models.py无法对app进行import,from app import db,以及import db,以及from . import db ,都不行,请问是否不能这样引用
2.代码结构如下:
我想在 app里的models.py里imprt app的db,但一直不成功,当我用 from . import db时,报错是 Attempted relative import in non-package,当用 from app import db的时候,报错是cannot import name db
__init__的代码如下:
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