2016-10-01 41 views
0

我正在呈現一個頁面供用戶瀏覽練習網站上的廣告列表。在「瀏覽」頁面上,我有三種單獨的表單,其中兩種是選擇字段,另一種是文本字段。選擇字段允許用戶更改位置或交易類型,文本字段允許用戶更改他們正在搜索的內容。我想做這個純HTML,而不是使用flask-wtf。我已經成功地獲得了可以更改位置工作的部分,但只要爲其他選項添加代碼,就會中斷,並且我得到一個400錯誤的請求錯誤。當我添加更改項目和交易類型的表單時,爲什麼會出現這種情況,任何建議或指導都不起作用?python-flask html form help/suggestions

@app.route('/find-it/', methods=['GET','POST']) 
def browse(): 
    try: 

    location = session.get('location', None) 
    transType = session.get('transType', None) 
    item = session.get('item', None)   

    data = browseQuery() 

    if request.method == 'POST': 
     if request.form['changeLocation'] != '': 
      print('location changed') 
      location = request.form['changeLocation'] 
      session['location'] = location 
      return redirect(url_for('browse', location=location)) 
     elif request.form['changeType'] != '': 
      print('type changed') 
      transType = request.form['changetype'] 
      session['transType'] = transType 
      return redirect('browse', transType=transType) 
     else: 
      if request.form['changeItem'] != '': 
       print('item changed') 
       item = request.form['changeItem'] 
       session['item'] = item 
       return redirect(url_for('browse', item=item)) 

     return render_template('all-classifieds.html',location=location,transType=transType, data=data) 
except Exception as e: 
    return (str(e)) 

HTML:

<form method="post" id="changeLocation" name="changeLocation" action=""> 

        <select name="changeLocation" id="changeLocation" style="margin-right: 5%; float: left;"> 
         <optgroup label="Where?"> 
          <option selected style="display:none;color:#eee;"></option> 
          <option>option 1</option> 
          <option>option 2</option> 

         </optgroup></select></form> 


<button type="submit" id="submit" form="changeLocation" style="padding: 0 3px; float:left;" 
         class="btn btn-info btn-sm"> 
        <i class="glyphicon glyphicon-search"></i> 
       </button> 




<form method="post" name="changeType" id="changeType"> 


        <select name="changeType" id="changeType" style="margin-right: 5%; float: left;"> 
         <optgroup label="..."> 
          <option selected style="display:none;color:#eee;"></option> 
          <option>option 1</option> 
          <option>option 2</option> 
          <option>option 3</option> 

         </optgroup> 
        </select> 



<form method="post" name="changeType" id="changeType"> 


        <select name="changeType" id="changeType" style="margin-right: 5%; float: left;"> 
         <optgroup label="Look for things that are..."> 
          <option selected style="display:none;color:#eee;"></option> 
          <option>asdf</option> 
          <option>asdfasdf</option> 
          <option>asdfasdfasdf</option> 

         </optgroup> 
        </select></form> 

<button type="submit" id="submit" form="changeType" style="padding: 0 3px; float:left;" 
         class="btn btn-info btn-sm"> 
        <i class="glyphicon glyphicon-search"></i> 
       </button> 


<form method="post" name="changeItem" id="changeItem"> 
        <input type="text" name="changeItem" value="" id="changeItem" placeholder=""/> 
       </form> 

<button type="submit" id="submit" form="changeItem" style="padding: 0 3px; float:left;" 
         class="btn btn-info btn-sm"> 
        <i class="glyphicon glyphicon-search"></i> 
       </button> 
+0

你爲什麼不使用debug = True,那麼你就會看到實際的錯誤是什麼? –

+0

對此很新穎。我確實有debug = True。不知道如何使用它我猜? @DanielRoseman我沒有得到一個回溯頁面。它只是說當我提交時,頁面上有400個錯誤的請求,並且全是 – user3344239

+0

當你收到一個GET請求時,你有代碼來處理加載頁面嗎?我在這裏看到'POST'的代碼,你有沒有更多顯示? – coralvanda

回答

1

你問的問題:

隨着

if request.form['changeLocation'] != '': 

你可能試圖找出是否使用該特定形式。問題是:當使用正確的表單時,該檢查工作正常,但如果使用了不同的表單,request.form字典將引發關鍵錯誤。但這不是正常的KeyError。 Flask會將該「特殊」錯誤轉化爲400 Bad Request

要檢查是否有某些關鍵是存在於窗體數據使用in操作

if "changeLocation" in request.form: 
    # you can use request.form["changeLocation"] here 
elif "changeType" in request.form: 
    # here request.form["changeType"] is present 
... 

另一個問題:

此行

render_template('all-classifieds.html',location=location,transType=transType, data=data) 

將無法​​正常工作,因爲locationtransTypeifb中定義鎖。如果塊沒有執行,它將會失敗。

不同的東西:

else: 
    if condition: 
     do_something 

可以改寫爲

elif condition: 
    do_something 
+0

謝謝@Wombatz – user3344239