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 %}

我应该做什么?

4

1 回答 1

1

Bad Request通常是访问不存在的参数的结果,例如request.form['organization_id']当表单中没有具有该名称的元素时。

您的调查路线将始终尝试organization_id从表单中检索,因为您将其设置为None,并且在测试之前没有任何更改。在第二个 Post 中,您的模板根本没有创建organization_id元素,因为该值是从前一个 Post 传递的,因此当您survey()仍然尝试检索它时会看到错误。

您需要某种方法将您提交的值从一个步骤传递到下一步。例如,您可以将它们写入表单中的隐藏或禁用字段,这样您就可以在每次发布后抓取它们并将它们发送回模板,或者将您的状态存储在其他位置,例如session.

于 2013-01-16T22:00:23.787 回答