对用户帖子做出反应

时间:2013-01-15 03:57:54

标签: python flask jinja2 flask-sqlalchemy

无论下面我的具体问题是什么,都是一种有效的方式,可以多次响应同一页面上的用户帖子。那么在页面上的每个渐进式帖子上都可以捕获新请求并显示新信息? 如果我没有正确描述问题,请原谅我。

我正在尝试构建一个对用户帖子做出反应的页面。但我遇到了:

  

错误请求

     

浏览器(或代理)发送了此服务器无法发出的请求   理解。

我猜是因为在我目前的解决方案中:

@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 个答案:

答案 0 :(得分:1)

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

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

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