我不知道如何解决我在使用 Flask 时从 Python 代码中遇到的这个问题:
@app.route('/addEvent/', methods=['POST'])
def addEvent():
@app.route('/deleteEvent/', methods=['POST'])
def addEvent():
错误信息:
AssertionError: View function mapping is overwriting an existing endpoint function: addEvent
21:50:57 web.1 | Traceback (most recent call last):
我试着理解这个页面:http://flask.pocoo.org/docs/0.10/patterns/viewdecorators/
还有这篇文章AssertionError: View function mapping is overwriting an existing endpoint function: main
但是我不明白。有人可以告诉我如何为我的代码解决这个问题吗?
最佳答案
重命名第二个函数;它也被称为 addEvent
;我建议改为使用 deleteEvent
:
@app.route('/deleteEvent/', methods=['POST'])
def deleteEvent():
端点 名称通常取自您用@app.route()
修饰的函数;您还可以通过告诉装饰器您想要使用什么名称来显式地为您的端点指定一个不同的名称:
@app.route('/deleteEvent/', methods=['POST'], endpoint='deleteEvent')
def addEvent():
这会让您坚持为函数使用相同的名称。在这种特定情况下,这不是一个好主意,因为一个函数替换了另一个函数,而第一个函数的唯一引用是在 Flask URL 映射中。
另见 Flask.route()
documentation :
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint.
关于python - 断言错误 : View function mapping is overwriting an existing endpoint function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819876/