Flask / Jinja2表单提交按钮不起作用

时间:2015-03-24 18:30:03

标签: flask jinja2

我有一个简单的表单,用户输入两个日期。我的输入正确传递,但提交按钮不起作用。这是我的观点:

# GLASS ------------------------------------------------------------------------
class NameForm(Form):
    starts_on = StringField('Starts', validators=[Required()])
    ends_on = StringField('Ends', validators=[Required()])
    submit = SubmitField('Go')

@app.route('/glass/', methods = ['GET', 'POST'])
def glasses():
    starts_on = None
    ends_on = None
    results = None
    form = NameForm()

    if form.validate_on_submit():
        starts_on = form.starts_on.data
        ends_on = form.ends_on.data
        # SQL takes starts_on, and ends_on as inputs
        results, start_date, end_date, companies_tracked = diagUserActs.userTime(starts_on, ends_on, diagUserActs.companies)
        form.starts_on.data = ''
        form.ends_on.data = ''

    return render_template('glass.html', form = form, results = results)

这是我的模板:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

<!--{% block title %}Glasses{% endblock %}-->

{% block content %}

<form class="form-inline" method="post" role="form">
    {{ wtf.form_field(form.starts_on) }}
    {{ wtf.form_field(form.ends_on) }}
    {{ wtf.form_field(form.submit) }}
</form>

奇怪的是,下面这个方法有效,但我想单独列出表单元素,这样我就可以更好地控制它的呈现:

<form class="form-inline" method="post" role="form">
    {% for field in form %}
        {{ field.label }}
        {{ field(placeholder="YYYY-MM-DD") }}
    {% endfor %}
</form>

1 个答案:

答案 0 :(得分:0)

之前我做过的就是使用这样的东西:

{% macro render_field(field, class='None') %}
    <div class="pure-control-group">
        {{ field.label(class_=class)|safe }}
        {{ field(class_=class)|safe }}
        {% if field.errors %}
            <p class="errors">
                {% for error in field.errors %}
                    <p>
                        {{ error }}
                    </p>
                {% endfor %}
            </p>
        {% endif %}
    </div>
{% endmacro %}

然后使用类似这样的东西:

{{ render_field(form.username) }}

表格是这样的:

form = forms.Login(request.form)

这里的表格似乎有问题:

{{ wtf.form_field(form.starts_on) }}

我从没见过用过这种方式。