【问题标题】:use many submit buttons in the same form在同一个表单中使用多个提交按钮
【发布时间】:2017-10-04 08:08:01
【问题描述】:

我无法访问框架中的所有按钮。它仅适用于直方图按钮。这是我想在 Post 方法中访问它的表单。

 <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代码。

 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

我做错了什么?

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    它不会像你写的那样工作。 request.form 中只会包含您发送的提交按钮,如果您尝试使用其他按钮之一的名称,则会收到错误消息。

    另外,request.form.get 是一个函数,而不是字典。您可以使用request.form.get("Histogram")——如果使用了Histogram按钮,则返回该按钮的值,否则返回None

    不要给按钮不同的名称,而是使用相同的名称但不同的值。

    <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 代码可以是:

    if request.form['action'] == 'Download':
        ...
    elif request.form['action'] == 'Histogram':
        ...
    elif request.form['action'] == 'Search':
        ...
    else:
        ... // Report bad parameter
    

    【讨论】:

    • 我的 request.form 没有 post 属性。在表单本身中找到操作。
    • 您的表单是否输入了name="action"
    【解决方案2】:

    虽然对不同的提交使用相同的名称可以工作(如上一个答案中所建议的),但对于 i18n 来说可能不太方便。

    您仍然可以使用不同的名称,但您应该检查以不同的方式处理它们:

    if 'Download' in request.form:
        ...
    elif 'Histogram' in request.form:
        ...
    elif 'Search' in request.form:
        ...
    else:
        ... // Report bad parameter
    

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2017-05-03
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多