2013-01-15 86 views
0

無論我的具體問題如何,對用戶多次回覆帖子的有效方式是在同一頁上發佈。因此,頁面上的每個漸進式帖子都可以捕獲新請求並顯示新信息? 如果我沒有正確描述問題,請原諒我。對用戶帖子作出反應

我試圖建立一個頁面來反應用戶的帖子。但我跑成:

壞請求

瀏覽器(或代理)發送的請求,該服務器無法理解 。

我猜是因爲我目前的解決方案:

@app.route('/survey', methods = ['GET', 'POST'])$        
@contributer_permission.require(403)$ 
def survey(): 
    organization_id = None 
    survey_header_id = None 
    survey_section_id = None 

    organization_selected = None 
    survey_header_selected = None 
    survey_section_selected = None 

    if request.method== 'POST': 
     if not organization_id: 
      organization_id = request.form['organization_id'] 
      organization_selected = Organization.query.get(organization_id) 

     elif not survey_header_id: 
      survey_header_id = request.form['survey_header_id'] 
      survey_header_selected = SurveyHeader.query.get(survey_header_id) 
     elif not survey_section_id: 
      pass 
     else: 
      pass 

    return render_template('survey.html', 
     organization_class = Organization, 
     organization_selected = organization_selected, 
     organization_id = organization_id, 
     survey_header_id = survey_header_id, 
     survey_header_selected = survey_header_selected, 
     survey_section_id = survey_section_id, 
     survey_section_selected = survey_section_selected) 

一旦我接收攜帶survey_header_id職。它重新循環和

organization_id becomes none 

這裏是伴隨HTML/JSON

{% extends "base.html" %} 
{% block content %} 
    <div class ="entries"> <!-- should be a DIV in your style! --> 
    <form action="{{url_for('survey') }}" method="post" class="add-entry"/> 
      <dl> 
      {% if not organization_id %} 
       {% for organization in organization_class.query.all() %} 
        <dt><input type="radio", name="organization_id", 
        value="{{ organization.id }}"/>{{ organization.name }}</dt> 
       {% endfor %} 
       <dt><input type ="submit", name="submit_organization"/> 
      {% elif not survey_header_id %} 
       <h1>{{ organization_selected.name }}</h1> 
       {% for survey_header in organization_selected.survey_headers.all() %} 
        <dt><input type="radio", name="survey_header_id" 
        value="{{ survey_header.id }}"/>{{ survey_header.name }} 
       {% endfor %} 
       <dt><input type ="submit", name="submit_survey_header"/> 
      {% elif not survey_section_id %} 
       <p>hi</p> 
      {% else %} 
      {% endif %} 
      <dl> 
    </form> 
    </div> 
{% endblock %} 

我應該怎麼做?

回答

1

Bad Request通常是訪問不存在的參數的結果,如request.form['organization_id'],表單中沒有具有該名稱的表單中的元素。

您的調查路線將始終嘗試從表單中檢索organization_id,因爲您將其設置爲None,在測試之前沒有什麼可以更改。在第二篇文章中,您的模板完全不會創建organization_id元素,因爲該值是從前一篇文章傳遞的,因此您在survey()仍然嘗試檢索該文章時看到錯誤。

您需要某種方式將您提交的值從一步傳遞到下一步。例如,您可以將它們寫入表單中隱藏或禁用的字段,以便您可以抓取它們並在每個Post後將它們發送回模板,或將其狀態存儲在其他位置,如session