我的代码遇到了几个小时的问题。
我确定这是由循环导入问题引起的,但是我还有另一个问题。
我有两个蓝图,即auth和main。我只返回了render_template
而没有任何数据传递,一切顺利。但是,当我尝试在main.index
中显示某些内容时,突然出现了错误。
我想知道为什么它在循环导入问题中仅在auth.views
中能正常工作吗?
我的树结构
app
|- auth
|- __init__.py
|- forms.py
|- views.py
|- main
|- __init__.py
|- forms.py
|- views.py
|- template
|- __init__.py
|- models.py
config.py
manage.py
datta.sqlite
在auth.__init__.py
中:
from flask import Blueprint
auth = Blueprint("auth", __name__)
from . import views
和auth.views.py
中from app.auth import auth
from app.models import *
from manage import app
@auth.route('/')
def index():
page = request.args.get("page", 1, type=int)
articles = Article.query.order_by(Article.update_time.desc()).paginate(
page, app.config["ARTICLES_PER_PAGE"], False)
next_url = url_for('auth.index', page=articles.next_num if articles.has_next else None)
prev_url = url_for('auth.index', page=articles.prev_num if articles.has_prev else None)
return render_template('index.html', articles=articles.items,
next_url=next_url, prev_url=prev_url)
在main.__init__.py
中:
from flask import Blueprint
main = Blueprint("main", __name__)
from . import views
在main.views.py
中:
from app.main import main
from app.models import *
from manage import app
@main.route('/')
def index():
page = request.args.get("page", 1, type=int)
articles = Article.query.order_by(Article.update_time.desc()).paginate(
page, app.config["ARTICLES_PER_PAGE"], False)
next_url = url_for('main.index', page=articles.next_num if articles.has_next else None)
prev_url = url_for('main.index', page=articles.prev_num if articles.has_prev else None)
return render_template('index.html', articles=articles.items,
next_url=next_url, prev_url=prev_url)
在app.__init__.py
中:
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
Config.init_app(app)
...
from app.main import main
app.register_blueprint(main)
from app.auth import auth
app.register_blueprint(auth, url_prefix='/auth')
return app
在manage.py
中from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager, Shell
from app import create_app, db
from app.models import *
app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
def make_shell_context():
return dict(db=db, ArticleType=ArticleType, Source=Source,
Article=Article, User=User, Menu=Menu,
ArticleTypeSetting=ArticleTypeSetting)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command("db", MigrateCommand)
if __name__ == '__main__':
app.run(debug=True)
我的回溯如下:
Traceback (most recent call last):
File "C:/Users/bohn/Desktop/1pycharm workspace/BlogPoweredByFlask/manage.py", line 7, in <module>
app = create_app()
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\__init__.py", line 30, in create_app
from app.main import main
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\main\__init__.py", line 5, in <module>
from . import views
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\main\views.py", line 5, in <module>
from manage import app
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\manage.py", line 7, in <module>
app = create_app()
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\__init__.py", line 33, in create_app
from app.auth import auth
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\auth\__init__.py", line 5, in <module>
from . import views
File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\auth\views.py", line 9, in <module>
from manage import app
ImportError: cannot import name 'app'
答案 0 :(得分:1)
克服此问题的方法是使用flask.current_app
而不是尝试导入app
。
在main.views中,替换
from manage import app
使用
from flask import current_app
然后,使用app.config[...]
代替current_app.config[...]