Skip to main content
 首页 » 编程设计

python之Flask之蓝图名称发生冲突

2024年10月24日8thcjp

我遇到了一个问题。控制台说发生了蓝图的名称冲突,但我认为这不属于 flask 引导问题。我相信我有一些配置或未知的东西让我遇到了问题。

Traceback (most recent call last): 
  File "manage.py", line 10, in <module> 
    app = create_app() 
  File "D:\TONY\GitHub\private-home-flask-cuisine\db-example\app\__init__.py", line 79, in create_app 
    init_extensions(app) 
  File "D:\TONY\GitHub\private-home-flask-cuisine\db-example\app\__init__.py", line 46, in init_extensions 
    extension.init_app(app=app) 
  File "D:\TONY\GitHub\private-home-flask-cuisine\db-example\env\lib\site-packages\flask_bootstrap\__init__.py", line 137, in init_app 
    app.register_blueprint(blueprint) 
  File "D:\TONY\GitHub\private-home-flask-cuisine\db-example\env\lib\site-packages\flask\app.py", line 62, in wrapper_func 
    return f(self, *args, **kwargs) 
  File "D:\TONY\GitHub\private-home-flask-cuisine\db-example\env\lib\site-packages\flask\app.py", line 885, in register_blueprint 
    (blueprint, self.blueprints[blueprint.name], blueprint.name) 
AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x034EA230> and <flask.blueprints.Blueprint object at 0x0349 
7770>.  Both share the same name "bootstrap".  Blueprints that are created on the fly need unique names. 

这是我的代码。我不知道会发生什么。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
#  @first_date    20160129 
#  @date          20160129 
#  @version       0.0 
"""Init flask app 
""" 
from flask import Flask 
 
from flask.ext.bootstrap import Bootstrap 
from flask.ext.sqlalchemy import SQLAlchemy 
from flask.ext.marshmallow import Marshmallow 
from flask.ext.login import LoginManager 
 
import configs 
 
 
# Initializing process: This package is main flask app 
app = Flask(__name__) 
app.config.from_object(configs.CONFIGS['default']) 
 
 
# Initializing process: This extesion list is created extension object 
bootstrap = Bootstrap() 
db = SQLAlchemy() 
ma = Marshmallow() 
 
 
login_manager = LoginManager() 
login_manager.session_protection = "strong" 
login_manager.login_view = 'users.login' 
 
 
def init_extensions(app): 
    '''Initializing the flask app with extensions''' 
    # Extension List: Wrap up the all extensions 
    extensions = ( 
        bootstrap, 
        db, 
        ma,  # Warning: Flask-SQLAlchemy must be initialized before Flask-Marshmallow. 
        login_manager, 
    ) 
    # Initializing process: Start to initial each extension 
    for extension in extensions: 
        extension.init_app(app=app) 
 
 
 
def init_blueprints(app): 
    '''Initializing the flask app with blueprints''' 
    # Blueprint source: Import the blueprints and note these sources 
    from .views import users 
 
    from .web import web_bp 
 
    # Blueprint List: Wrap up the all blueprints 
    buleprints = ( 
        dict(blueprint=users.users_bp, url_prefix='/users'), 
 
        dict(blueprint=web_bp, url_prefix=''), 
    ) 
 
    # Initializing process: Start to initial each blueprint 
    for blueprint in buleprints: 
        app.register_blueprint(**blueprint) 
 
 
def init_error_handlers(app): 
    '''import error handler function''' 
    from .error_handlers.built_in_exception_handlers import * 
    from .error_handlers.status_code_handlers import * 
    from .error_handlers.sqlachelmy_handlers import * 
 
 
def create_app(): 
    '''It's a factory.''' 
    # Initializing process: Initializing the main flask app with extensions 
    init_extensions(app) 
 
    # Initializing process: Initializing the main flask app with blueprints 
    init_blueprints(app) 
 
    # Initializing process: import error handlers 
    init_error_handlers(app) 
 
    return app 

或者任何人都可以告诉我如何在应用程序中查找蓝图对象并让我跟踪错误。谢谢。

请您参考如下方法:

您的应用似乎正在注册 Bootstrap 蓝图两次。 Bootstrap 的源代码中有一行确实在 init_app()

中注册了它

尝试从这里删除它:

    ... 
    bootstrap, 
    db, 
    ma,  # Warning: Flask-SQLAlchemy must be initialized before Flask-Marshmallow. 
    login_manager, 

或者来自这个元组:

# Blueprint List: Wrap up the all blueprints 
    buleprints = ( 
        dict(blueprint=users.users_bp, url_prefix='/users'), 
 
        dict(blueprint=web_bp, url_prefix=''), 
    )