2016-07-04 21 views
5

Dưới đây là cách bố trí dự án của tôi:'chức năng' đối tượng không có thuộc tính 'name' khi đăng ký kế hoạch chi tiết

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

Đây là in của tôi

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 

Làm thế nào tôi đăng ký ở __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) 

Đây là lỗi:

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

Lưu ý rằng lỗi bạn đã tạo không dành riêng cho Flask hoặc bản thiết kế của nó. Tuy nhiên, nếu bạn nghĩ rằng tài liệu cần được cải thiện, bằng mọi cách, hãy tham gia vào dự án một cách tích cực thông qua [issue pages] của dự án (https://github.com/pallets/flask/issues). – jonrsharpe

Trả lời

12

Bạn đeo mặt nạ kế hoạch chi tiết bằng cách tái sử dụng tên:

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

Bạn không thể có cả các tuyến đường và các kế hoạch chi tiết sử dụng cùng tên; bạn đã thay thế bản thiết kế và đang cố gắng đăng ký chức năng định tuyến.

Đổi tên nó:

health_blueprint = Blueprint('health', __name__) 

và đăng ký rằng:

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

tên kế hoạch chi tiết của bạn là tương tự với tên hàm của bạn, hãy thử đổi tên tên hàm để thay thế.

health = Blueprint('health', __name__) 
@health.route("/health", methods=["GET"]) 
def check_health(): 
Các vấn đề liên quan