2017-05-05 1 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> 

여기 내 파이썬 코드입니다.

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> 

그런 다음 파이썬 코드가 될 수 있습니다

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

대단히 감사합니다! 그것은 완벽하게 작동합니다! – Andreea

관련 문제