【发布时间】: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 方法。
谢谢,
【问题讨论】: