即使指定了POST,烧瓶也会返回405方法不允许

时间:2020-03-27 19:15:52

标签: python flask

上传到我的upload_file路线时,出现405方法不允许错误。我已指定该路由接受GET和POST方法,所以我不确定为什么它不起作用。

@app.route('/upload', methods=["GET, POST"])
def upload_file():
    if request.method == 'GET':
        return render_template("home.html")
    elif request.method == 'POST':
        if 'file' not in request.file:
            return render_template("home.html")

        file = request.files['file']

        if file.filename == '':
            return render_template("home.html")

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            converted_file = convert(file)
            return render_template("home.html", converted_file=converted_file, img_src=UPLOAD_FOLDER+filename)
<form  method="post" enctype="multipart/form-data" action="/upload" >
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>    

1 个答案:

答案 0 :(得分:1)

@app.route('/upload', methods=["GET, POST"])

应为:

@app.route('/upload', methods=["GET", "POST"])

您要提供一个带有一个字符串“ GET,POST”的列表,而不是一个带有两个字符串的列表:“ GET”和“ POST”。

PS: 如此处所述:Do not use run() in production.

请勿在生产设置中使用run()。它无意满足生产服务器的安全性和性能要求。相反,请参阅WSGI服务器建议的部署选项。

请阅读Deployment Options