2015-09-22 63 views
-1

我燒瓶應用視圖功能:什麼原因導致這個不好的請求?

@app.route("/database_filter", methods=['POST', 'GET']) 
def database_filter(): 

    error = '' 
    points = '' 
    time_search = False 
    hdop_search = False 
    points_len = 0 
    conn = sqlite3.connect(database) 
    c = conn.cursor() 

    if request.method == 'POST': 

     '''If all datetime forms are filled out and the start is not after the end, set time_search True''' 

     if request.form['startdate'] and \ 
      request.form['starttime'] and \ 
      request.form['enddate'] and \ 
      request.form['endtime']: 
      start = request.form['startdate'] + 'T' + request.form['starttime'] 
      end = request.form['enddate'] + 'T' + request.form['endtime'] 
      if datetime_object(start) > datetime_object(end): 
       error = '**Start value selected must occur before End value.' 
       return render_template('database_filter.html', error=error) 
      else: 
       time_search = True 
       print 'time search: ' + str(time_search) 

     if request.form['hdop_oper'] and request.form['hdop']: 
      hdop_search = True 
      print 'hdop and hdop_oper exist' 
      hdop_oper = request.form['hdop_oper'] 
      hdop = float(request.form['hdop']) 


     '''Prepare search statement based on forms selected''' 

     if time_search == True and hdop_search == True: 
      print 'Time and hdop:' 
      search_string = 'SELECT * from (SELECT TSTAMP, LAT, LON, FILE, HDOP FROM POINTS WHERE TSTAMP BETWEEN "%s" and "%s") WHERE HDOP %s %.1f ORDER BY TSTAMP DESC'%(start, end, hdop_oper, hdop) 
      print search_string 

     if time_search == False and hdop_search == True: 
      print 'Hdop only' 
      search_string = 'SELECT TSTAMP, LAT, LON, FILE, HDOP FROM POINTS WHERE HDOP %s %.1f ORDER BY TSTAMP DESC'%(hdop_oper, hdop) 
      print search_string 

     if time_search == True and hdop_search == False: 
      print 'Time only' 
      search_string = 'SELECT TSTAMP, LAT, LON, FILE FROM POINTS WHERE TSTAMP BETWEEN "%s" and "%s" ORDER BY TSTAMP DESC'%(start, end) 
      print search_string 

     if time_search == False and hdop_search == False: 
      print 'no time no hdop' 
      error = 'No search criteria selected.' 
      search_string = 'SELECT TSTAMP, LAT, LON, FILE FROM POINTS ORDER BY TSTAMP DESC' 
      print search_string 

     '''Execute SQL search and return dictionary of points''' 

     cur = c.execute(search_string) 

     points = [dict(TSTAMP=row[0][11:20], DATE=row[0][0:10], 
      LAT=row[1], 
      LON=row[2], 
      FILE = row[3]) for row in cur.fetchall()] 

     points_len = len(points) 

     return render_template('database_filter.html', error=error, points=points, points_len=points_len) 

    return render_template('database_filter.html', error=error, points=points, points_len=points_len) 

和HTML頁面來呈現它:

<!--Filter Search criteria form --> 
<form action="database_filter" id="database_filter" method="post"> 
    <p id="basicExample"> 
     <!-- Date/time range form; requires js library at bottom --> 
     <input type="test" placeholder="Start Date" class="date start" name="startdate" value="{{ request.form.startdate }}"> 
     <input type="test" placeholder="Start Time" class="time start" name="starttime" value="{{ request.form.starttime }}"> to 
     <input type="test" placeholder="End Date" class="date end" name="enddate" value="{{ request.form.enddate }}"> 
     <input type="test" placeholder="End Time" class="time end" name="endtime" value="{{ request.form.endtime }}"> 
    </p> 

    <br> 

    <strong>HDOP Filter</strong><br> 
    <select name="hdop_filter"> 
     <option>HDOP</option> 
    </select> 


    <select id="hdop_oper" name="hdop_oper"> 
     <option>&lt;</option> 
     <option>&gt;</option> 
     <option>&ge;</option> 
     <option>&le;</option> 
     <option>&#61;</option> 
     <option selected disabled value=''></option> 
    </select> 


     HDOP Value; 
     <input type="number" id="hdop" name="hdop" min="0" max="2" step=".1"> 


<input class="btn-default" type="submit" value="Update Search"> 
<br> 
<strong>*Number of points found: </strong>{{points_len}} 
</form> 
<br> 
<br> 





    <h4>{{error}}</h4> 
    <br> 
    <p><strong>Date =</strong> YYYY-MM-DD <strong>Time =</strong> HH:MM:SS</p> 


    {% for point in points %} 
    <pre> 
    <strong>Date:</strong> {{ point.DATE }} <strong>Time:</strong> {{ point.TSTAMP }} 
    <strong>Latitude:</strong> {{ point.LAT }} <strong>Longitude:</strong> {{ point.LON }} 
    <strong>Source File:</strong> {{point.FILE }}</pre> 
    {% endfor %} 





      <!-- datetime formatting https://codex.wordpress.org/Formatting_Date_and_Time --> 
      <!-- http://jonthornton.github.io/jquery-timepicker/ --> 
      <script> 
       $('#basicExample .time').timepicker({ 
        'scrollDefault': 'now', 
        'showDuration': true, 
        'timeFormat': 'H:i:s' 
       }); 

       $('#basicExample .date').datepicker({ 
        'scrollDefault': 'now', 
        'format': 'yyyy-mm-dd', 
        'autoclose': true 
       }); 
       $(document).ready(ready); 

       var basicExampleEl = document.getElementById('basicExample'); 
       var datepair = new Datepair(basicExampleEl); 
      </script> 
      <!-- END Script for date time picker --> 

任我選擇所有的日期時間值,並沒有HDOP值,瓶返回一個「壞請求」。但是,如果我只選擇hdop過濾器信息並且沒有日期時間,它會按預期返回數據。任何線索?除了他們的表單類型,我發現其他兩個沒有區別。

感謝您的關注!

+1

在大視圖方法的某處存在未處理的異常。你有沒有嘗試開啓調試? 'app.run(debug = True)' – IanAuld

+0

是的,它現在開啓。但是,它沒有顯示異常,只是提供了一個錯誤的請求頁面。 –

+1

您是否檢查過Web服務器錯誤日誌?數據包嗅探器輸出? –

回答

0

錯誤是在方法調用中獲取hdop_oper和hdop的表單值。

正確的方式來獲得在瓶的可選形式是使用以下命令:

if request.form.get('hdop_oper') and request.form.get('hdop'): 

相反的:

if request.form['hdop_oper'] and request.form['hdop']: 

我想指出的是,瓶的回溯功能並沒有提出'更糟糕的'例外情況,讓你猜猜真正的問題是什麼。

我明白這不是用來調試你的代碼,但我確定有一些Flask pro會在這裏發現這個Flask異常。如果沒有別的,有人可能會在未來發現這有幫助。

相關問題