gpt4 book ai didi

python - “功能”对象在注册蓝图时没有属性 'name'

转载 作者:太空狗 更新时间:2023-10-29 18:15:12 25 4
gpt4 key购买 nike

这是我的项目布局:

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

这是我的作品

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

我如何在 __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)

这里是错误:

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'

最佳答案

您通过重新使用 View 函数的名称来屏蔽引用Blueprint 实例的health 全局名称:

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

您不能让路线 View 功能和蓝图使用相同的名称;您替换了引用蓝图的全局名称 health 并尝试为相同的全局名称注册路由函数。

为蓝图使用不同的名称:

health_blueprint = Blueprint('health', __name__)

并注册:

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

或为 View 函数使用不同的名称(此时端点名称也会更改,除非您在 @health.route(... ) 装饰器).

关于python - “功能”对象在注册蓝图时没有属性 'name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com