【问题标题】:What is the cause of the Bad Request Error when handling a form in flask?在烧瓶中处理表单时出现错误请求错误的原因是什么?
【发布时间】:2018-01-29 12:30:16
【问题描述】:

在阅读了许多类似的听起来问题和相关的 Flask 文档后,我似乎无法弄清楚是什么在提交表单时产生了以下错误:

400 错误请求

浏览器(或代理)发送了此服务器无法发送的请求 明白。

@app.route('/uploadtodos', methods=['POST','GET'])
def uploadtodos():
    file= request.files['inputFile']

    newFile = FileContents(name=file.filename, data= file.read())
    db.session.add(newFile)
    db.session.commit()
    return render_template('uploadtodos.html') 



<form method="POST" action ="/uploadtodos" enctype="multipart/form-data" >
        <label for="inputFile"></label>
        <input type="file" name="inputFile">
        <!--input type="submit" -->
        <button type="submit">submit</button> 
    </form>

【问题讨论】:

  • flask开发服务器的控制台有信息吗?
  • 您有一个 form 发出 POST 请求,但您没有在 uploadtodos 定义中直接指定它

标签: python flask upload bad-request


【解决方案1】:

在我忘记了自己的一个“如果”中,问题又重新开始了。

@app.route('/uploadtodos', methods=['POST','GET'])
    def uploadtodos():
        if request.method =='POST':
            file= request.files['inputFile']

            newFile = FileContents(name=file.filename,data=file.read())
            db.session.add(newFile)
            db.session.commit()
            return 'Saved ' +file.filename + ' to the database!'
      return render_template('uploadtodos.html')

【讨论】:

    猜你喜欢
    • 2012-12-15
    • 2023-04-01
    • 2018-05-10
    • 2019-09-08
    • 2017-06-02
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多