2017-05-05 70 views
0

我無法訪問框架中的所有按鈕。它僅適用於直方圖按鈕。這裏是我想要在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 

我在做什麼錯了?

回答

3

它不會像你寫的那樣工作。只有您發送的提交按鈕將包含在request.form中,如果您嘗試使用其他按鈕之一的名稱,則會出現錯誤。

而不是給按鈕不同的名稱,使用相同的名稱,但使用不同的值。

<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.post['action'] == 'Download': 
    ... 
elif request.form.post['action'] == 'Histogram': 
    ... 
elif request.form.post['action'] == 'Search': 
    ... 
else: 
    ... // Report bad parameter 
+0

非常感謝您!它完美的工作! – Andreea

相關問題