目前我正在尝试使用login_required
装饰器来接受url端点是一个API,以确定返回。但是目前,它给了我一个错误,它覆盖了一个函数。但我不知道它压倒什么。
以下是运行run.py文件时产生的错误
$ python run.py
Traceback (most recent call last):
File "run.py", line 2, in <module>
from comicreader.app import app
File "/Users/JeremyZhang/Downloads/DeviantArt-Comic-Reader/comicreader/app.py", line 16, in <module>
app.register_blueprint(blueprints.fetch.fetch, url_prefix="/fetch")
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 889, in register_blueprint
blueprint.register(self, options, first_registration)
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/blueprints.py", line 153, in register
deferred(state)
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/blueprints.py", line 172, in <lambda>
s.add_url_rule(rule, endpoint, view_func, **options))
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/blueprints.py", line 76, in add_url_rule
view_func, defaults=defaults, **options)
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/Library/Python/2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 984, in add_url_rule
'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: fetch.decorator
我做错了什么?我之前已经问了一个类似的问题,但我认为将直接来源发布到我正在进行的项目中应该更容易弄清楚而不是玩猜字谜。
答案 0 :(得分:1)
你的装饰工作正常,这是一个演示:
Traceback (most recent call last):
File "run.py", line 2, in <module>
from apiapi.app import app
-> File "/var/www/python/apiapi.py", line 16, in <module>
app.register_blueprint(blueprints.fetch.fetch, url_prefix="/fetch")
错误可能与其他内容有关:
@fetch.route("/apipage")
我怀疑您必须在代码中多次定义此路由login_required
。
经过调查,问题是@login_required()
根据OP定义有一个位置必需的参数,所以修复它是这样使用装饰器:
@login_required(api=True)
#为默认参数值
{{1}}