2016-07-04 14 views
5

Ecco il mio layout del progetto:oggetto 'funzione' non ha alcun attributo 'nome' al momento della registrazione progetto

baseflask/ 
    baseflask/ 
     __init__.py 
     views.py 
     resources/ 
      health.py/ 
    wsgi.py/ 

Qui è la mia stampa

from flask import Blueprint 
from flask import Response 
health = Blueprint('health', __name__) 
@health.route("/health", methods=["GET"]) 
def health(): 
    jd = {'status': 'OK'} 
    data = json.dumps(jd) 
    resp = Response(data, status=200, mimetype='application/json') 
    return resp 

Come mi iscrivo in __init__.py:

import os 
basedir = os.path.abspath(os.path.dirname(__file__)) 
from flask import Blueprint 
from flask import Flask 
from flask_cors import CORS, cross_origin 
app = Flask(__name__) 
app.debug = True 

CORS(app) 

from baseflask.health import health 
app.register_blueprint(health) 

Ecco l'errore:

Traceback (most recent call last): 
    File "/home/ubuntu/workspace/baseflask/wsgi.py", line 10, in <module> 
    from baseflask import app 
    File "/home/ubuntu/workspace/baseflask/baseflask/__init__.py", line 18, in <module> 
    app.register_blueprint(health) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 62, in wrapper_func 
    return f(self, *args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 880, in register_blueprint 
    if blueprint.name in self.blueprints: 
AttributeError: 'function' object has no attribute 'name' 
+0

Nota che l'errore che hai fatto non è specifico di Flask o dei suoi progetti. Tuttavia, se si ritiene che la documentazione necessiti di miglioramenti, impegnarsi in ogni modo con il progetto in modo costruttivo attraverso la [pagina dei problemi] del progetto (https://github.com/pallets/flask/issues). – jonrsharpe

risposta

12

È mascherato il modello da ri-utilizzando il nome:

health = Blueprint('health', __name__) 
@health.route("/health", methods=["GET"]) 
def health(): 

Non si può avere sia il percorso e il progetto di utilizzare lo stesso nome; hai sostituito il progetto e stai provando a registrare la funzione del percorso.

Rinominarlo:

health_blueprint = Blueprint('health', __name__) 

e registrare che:

from baseflask.health import health_blueprint 
app.register_blueprint(health_blueprint) 
0
health = Blueprint('health', __name__) 
@health.route("/health", methods=["GET"]) 
def health(): 

tuo nome progetto è lo stesso con il tuo nome di funzione, prova a rinominare il nome della funzione, invece.

health = Blueprint('health', __name__) 
@health.route("/health", methods=["GET"]) 
def check_health(): 
Problemi correlati