【问题标题】:Flask Bad Request Error; ocurring because of multiple POST requests [duplicate]烧瓶错误请求错误;由于多个 POST 请求而发生 [重复]
【发布时间】:2018-11-18 00:30:21
【问题描述】:
 <form method = "POST">
         <div class=" col-lg-4 col-lg-4 col-lg-4 col-lg-4">
            <div class="box">
                <input type="text" name="image-url" placeholder="Image URL Link"style="color:black" required="required" value = "new"/>
              <textarea cols=80 rows=4 style="color:black" name = "description" placeholder="Place your description here" value = "new"></textarea>
                <button type="submit">Upload</button>
                </div>
             </div>
            </form>                
    {% for i in range(amount_of_images) %}
         <div class=" col-lg-4 col-lg-4 col-lg-4 col-lg-4">
              <div class="box">
                <img src="{{image[i]}}" alt="view" width = "300" height = "300"/>
            <form method = "POST">
                <textarea cols=80 rows=4 style="color:black" name = "update-description" value = "update">{{description[i]}}</textarea>
                    <button type="submit">Update Description</button>
            </form>

以上是我的 HTML/Jinja 代码

@app.route("/gallery-manager", methods = ["GET", "POST"])
def gallery_manager():
if request.method == "POST":    
    if(request.form["image-url"] and request.form["description"]) is not None:
        model.add_new_image(request.form["image-url"], request.form["description"])
        id, image, description, amount_of_images = model.get_gallery()
        return render_template("gallery-manager.html", image = image, description = description, amount_of_images = amount_of_images, id = id)


    if request.form['update-description'] is not None:
        print("hello")



id, image, description, amount_of_images = model.get_gallery()
return render_template("gallery-manager.html", image = image, description = description, amount_of_images = amount_of_images, id = id)

以上是我的 Python/Flask 代码...

问题是,当我点击更新描述提交按钮时,也就是在 html 代码中处理的第二个 POST,我收到 400 错误

Bad Request
The browser (or proxy) sent a request that this server could not understand.

我意识到当 POST 字段之一为空且无法找到时会发生此错误。我知道发生这种情况是因为当我单击第二个 POST 提交按钮时,它会运行第一个 POST 检查(request.form["image-url"] 等并发现它很满意,因此想要运行该代码,但不能因为update-description 仍为空。如何避免这种情况。

换句话说,我如何处理多个 POST 方法。

谢谢,

【问题讨论】:

    标签: python html flask jinja2


    【解决方案1】:

    这是因为您缺少表单中应该发送请求的操作部分

    <form  action="/whereYouWantToSendRequest" method="post">
    

    通过在上面替换它来添加您的 url 端点 whereYouWantToSendRequest

    这是两个请求

      <input type="text" name="image-url" value = "new"/>
        <input type="text" name="update-description" value = "update"/>
    

    并找出哪个请求

    if request.form["image-url"] == "new":
         somethinggg
    elif request.form["update-description"] =="update":
         sommmm
    

    【讨论】:

    • 我将它发送到 html 代码所在的原始页面并得到相同的错误。我使用 POST 方法处理程序将其发送到另一个页面,但仍然出现相同的错误。我的另一个 POST 请求没有任何动作并且工作正常,(它上面的那个)。
    • 我回答了你的问题。
    • 不抱歉,还是同样的错误。还是谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2017-06-02
    • 1970-01-01
    • 2018-02-15
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多