AssertionError: View function mapping is overwriting an existing endpoint function

AssertionError: View function mapping is overwriting an existing endpoint function

我不知道如何解决我在使用 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/

还有这个postAssertionError: View function mapping is overwriting an existing endpoint function: main

但是我不明白。有人可以告诉我如何为我的代码解决这个问题吗?

重命名第二个函数;它也被称为 addEvent;我建议改为 deleteEvent

@app.route('/deleteEvent/', methods=['POST'])
def deleteEvent():

endpoint 名称通常取自您用 @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.