请看https://blog.csdn.net/qq_41805514/article/details/80614701
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.route('/upload')
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return 'file uploaded successfully'
if __name__ == '__main__':
app.run(debug=True)
这个错误的引起的原因是定义了多个同名的视图函数
但是也有两个函数不是相同,但是也会报这种错误。
其原因是python的装饰器本质上就是函数的链式调用,比如下面写法
解决方法:
python自带的functools提供了解决方案
functools.update_wrapper(wrapper,wrapped[,assigned][,updated]
update_wrapper的功能就是让装饰器看起来像被装饰的方法,默认会将装饰方法的属性(name,module and doc)替换为被装饰方法的。
http://trytofix.github.io/2016/05/05/Flask%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%A3%85%E9%A5%B0%E5%99%A8%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98AssertionError-View-function-mapping-is-overwriting-an-existing-endpoint-function/
https://stackoverflow.com/questions/31900579/view-function-mapping-is-overwriting-an-existing-endpoint-function-when-using-a
以上是参考资料。