2016-12-06 58 views
0

我有一個頁面呈現用戶搜索的數據。我希望允許用戶隨時在頁面一側更改搜索憑據。出於某種原因,它只是爲「更換學校」部分工作。爲什麼代碼永遠不會到達其他搜索字段(例如,當我更改搜索類型時,沒有任何反應,沒有任何內容顯示到控制檯)。請忽略縮進錯誤,它在粘貼時變得混亂!處理更新的搜索表單

相關HTML:(所有名稱/ ID的文本字段匹配在肯定的Python代碼)

<form id="update" name="update" method="post" action="/find-it/"> 
<input type="submit" value="search" id="enter" form="update"> 

PYTHON:

 if request.method == 'POST': 

     if "updateSchool" in request.form: 
      print('school changed') 
      school = request.form['updateSchool'] 
      data = updateSchool(school) 

      return render_template('testBrowse.html', data=data) 

     elif "updateType" in request.form: 
      print('type changed') 
      newType = request.form['updateType'] 

      data = updateType(newType) 

      print('getting to update type') 

      return render_template('testBrowse.html', data=data) 

     elif "updateTitle" in request.form: 
      print('title changed') 

      newTitle request.form['updateTitle'] 

      data = updateTitle(newTitle) 

      return render_template('testBrowse.html', data=data) 

    return render_template('testBrowse.html', data=data) 

回答

1

第一個問題,我看到的是,有沒有updateSchool,updateType或updateTitle輸入元素的HTML格式。

如果我們假設它們全部三個以相同的形式存在,那麼您將永遠不會超過第一個if塊。

if "updateSchool" in request.form: 

檢查是否領域的被張貼,而不是是否被填充的形式存在。所以它總是會評估真實的。您可以使用此代替:

if request.form['updateSchool']: 

只有在updateSchool字段填充後,纔會評估爲真。

而且,你不需要

return render_template('testBrowse.html', data=data) 

多個呼叫你可以只留下一個在底部。無論上面的代碼發生什麼,它總是返回相同的模板和數據。

+0

非常感謝! – user3344239