使用许多相同形式的提交按钮 [英] use many submit buttons in the same form
问题描述
我无法访问框架中的所有按钮.它仅适用于直方图"按钮.这是我要在Post方法中访问它的表单.
I cannot access all the buttons from the frame. It is works only with the Histogram button.Here is my form which I want to access it in the Post method.
<form id="package_form" action="" method="post">
<div class="panel-body">
<input type ="submit" name="Download" value="Download">
</div>
<div class="panel-body">
<input type ="submit" name="Histogram" value="Histogram">
</div>
<div class="panel-body">
<input type ="submit" name="Search" value="Search">
</div>
</form>
这是我的python代码.
Here is my python code.
if request.method == 'GET':
return render_template("preview.html", link=link1)
elif request.method == 'POST':
if request.form['Histogram'] == 'Histogram':
gray_img = cv2.imread(link2,cv2.IMREAD_GRAYSCALE)
cv2.imshow('GoldenGate', gray_img)
hist = cv2.calcHist([gray_img], [0], None, [256], [0, 256])
plt.hist(gray_img.ravel(), 256, [0, 256])
plt.xlabel('Pixel Intensity Values')
plt.ylabel('Frequency')
plt.title('Histogram for gray scale picture')
plt.show()
return render_template("preview.html", link=link1)
elif request.form.get['Download'] == 'Download':
response = make_response(link2)
response.headers["Content-Disposition"] = "attachment; filename=link.txt"
return response
elif request.form.get['Search'] == 'Search':
return link1
我做错了什么?
推荐答案
它无法按您编写的方式工作.只有您发送的提交按钮才会包含在 request.form
中,如果您尝试使用其他按钮之一的名称,则会收到错误消息.
It won't work the way you've written it. Only the submit button you send will be included in request.form
, you'll get an error if you try to use the name of one of the other buttons.
此外, request.form.get
是一个函数,而不是字典.您可以使用 request.form.get("Histogram")
-如果使用了 Histogram
按钮,它将返回 Histogram
按钮的值,否则将返回无
.
Also, request.form.get
is a function, not a dictionary. You can use request.form.get("Histogram")
-- this will return the value of the Histogram
button if it was used, otherwise it will return None
.
不要使用不同的名称来命名按钮,而应使用相同的名称但使用不同的值.
Instead of giving the buttons different names, use the same name but different values.
<form id="package_form" action="" method="post">
<div class="panel-body">
<input type ="submit" name="action" value="Download">
</div>
<div class="panel-body">
<input type ="submit" name="action" value="Histogram">
</div>
<div class="panel-body">
<input type ="submit" name="action" value="Search">
</div>
</form>
然后您的Python代码可以是:
Then your Python code can be:
if request.form.action == 'Download':
...
elif request.form.action == 'Histogram':
...
elif request.form.action == 'Search':
...
else:
... // Report bad parameter
这篇关于使用许多相同形式的提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!