Chan Chan - 3 years ago 429
Python Question

How can i both register blueprint and add that app to flask-admin

my Code:

from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)

from user.user import mod as user
from user.models import User as userModel

app.register_blueprint(user, url_prefix='/user')

admin = Admin(app, name='My app')
admin.add_view(ModelView(userModel, db.session, name='userAdmin'))

from flask import Blueprint, json
from flask.views import MethodView

mod = Blueprint('user', __name__)

class UserAPI(MethodView):
def get(self):
users = [
{'nickname': 'Chan'},
{'nickname': 'Hzz'},
return json.dumps(users)

mod.add_url_rule('/users/', view_func=UserAPI.as_view('users'))

from app import db

class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)

def __init__(self, username, email):
self.username = username = email

def __repr__(self):
return "<User %s>" % self.username

i have a blueprint in my user app, and i've registered it, but when i want to add this to my admin to manage the user data, it throws the below exception:

Traceback (most recent call last):
File "", line 2, in <module>
from app import app
File "/home/chenhj/flask/multiapp/app/", line 21, in <module>
admin.add_view(ModelView(userModel, db.session, name='chj'))
File "/home/chenhj/.virtualenvs/multiapp/local/lib/python2.7/site-packages/flask_admin/", line 526, in add_view
File "/home/chenhj/.virtualenvs/multiapp/local/lib/python2.7/site-packages/flask/", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/home/chenhj/.virtualenvs/multiapp/local/lib/python2.7/site-packages/flask/", line 885, in register_blueprint
(blueprint, self.blueprints[],
AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x25e5d90> and <flask.blueprints.Blueprint object at 0x21b89d0>. Both share the same name "user". Blueprints that are created on the fly need unique names.

i am crazy about that

Answer Source

The collision is because you have a module name user and a blueprint called user. Rename the blueprint to user_blueprint. From the code it seems you have a folder called user, a module called user and a blueprint called user. You can avoid problems later on with some descriptive names. Otherwise it is just plain confusing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download