PerryDaPlatypus
PerryDaPlatypus

Reputation: 773

Blueprints in Flask "Attribute 'function' object has no attribute 'name'

Problem Description

Getting error message when trying to start Flask.

Traceback (most recent call last):
  File "./run.py", line 3, in <module>
    from app import app
  File "/home/xxxxxx/xxxx.xxxxxxx.com/ClientTracker/app/__init__.py", line 13, in <module>
    app.register_blueprint(admin)
  File "/home/xxxxx/xxxxx.xxxxxxx.com/ClientTracker/env/local/lib/python2.7/site-packages/flask/app.py", line 65, in wrapper_func
    return f(self, *args, **kwargs)
  File "/home/xxxxx/xxxxx.xxxxxxx.com/ClientTracker/env/local/lib/python2.7/site-packages/flask/app.py", line 958, in register_blueprint
    if blueprint.name in self.blueprints:
AttributeError: 'function' object has no attribute 'name'

This is a migration from a simpler hierarchy implementing Blueprints. I'm splitting out the function of the frontend and the admin panel.

I built this step by step and had both sides working fine. Started migrating (functions and routes). After moving some code, I started getting an error message (basically the same as above, but different line).

Troubleshooting

Code

#ClientTracker/run.py
#!env/bin/python

from app import app
app.run(host='0.0.0.0', port=8080, debug=False)

#ClientTracker/app/__init__.py

# Import flask and template operators
from flask import Flask, render_template

# Define the WSGI application object
app = Flask(__name__)

# Import a module / component using its blueprint handler variable (mod_auth)
#from app.mod_auth.controllers import mod_auth as auth_module
from app.admin.views import admin
from app.client.views import client

# Register blueprint(s)
app.register_blueprint(admin)
app.register_blueprint(client)

#ClientTracker/app/admin/views.py
from flask import render_template, request, Blueprint
from app import app
import MySQLdb
import datetime

admin = Blueprint(
    'admin',
    __name__,
    url_prefix='/admin',
    template_folder='templates',
    static_folder='static'
)

@admin.route('/')
def admin():
    return "ok"

I'm out of ideas.

Upvotes: 6

Views: 8282

Answers (2)

Harun Mbaabu Mwenda
Harun Mbaabu Mwenda

Reputation: 19

Your blueprint name is same with your function name, try to rename the function name instead.

Note that the blue print name and the function name can not be the same.

Make use if this tutorial to learn more about Blueprints, https://realpython.com/flask-blueprint/

Upvotes: 1

PerryDaPlatypus
PerryDaPlatypus

Reputation: 773

Ok, so as seems to happen, I spend an hour looking, another 15 mins composing a question and then after I hit post, I find the answer.

I found a post (https://github.com/pallets/flask/issues/1327) that had the answer.

Basically, you cannot have a function name with the same name as your Blueprint name. Seems obvious now, but certainly stumped me for a while.

In thinking about it, my original "working" state had a dummy function name serving the '/'. When I rolled back, I didn't roll back far enough.

Replaced def admin(): with def admin1(): (will fix this better in prod) and all worked.

I hope this post helps someone else. Please still feel free to comment. As always, the group is always smarter than the individual. Lastly, thanks for reading this far. :-)

Upvotes: 23

Related Questions