form.validate_on_submit也是false

时间:2015-07-19 08:51:36

标签: python flask wtforms flask-wtforms

那么,我们如何使用jinja和wtf表单显示日期时间小部件?

forms.py

class FoodForm(Form):
    food_name = StringField('Food Name', validators=[Required()])
    food_description = StringField('Food Description', validators=[Required()])
    price = FloatField('Price', validators=[Required()])
    **event_starts = DateTimeField('Event Starts', validators=[Required()])
    event_ends = DateTimeField('Event Ends', validators=[Required()])**
    food_types = ['Indian', 'American', 'Chinese', 'Italian', 'Other', 'French',
                  'Deli','Mediterranean', 'Japanese']
    food_choices = [(i+1, food_type) for i, food_type in enumerate(sorted(food_types))]
    cuisine_type = SelectField('Cuisine Type', choices=food_choices, validators=[Required()])
    submit = SubmitField('Submit')

jinja_template

     <form class="form form-horizontal" role="form" method="POST">
        {{ form.hidden_tag() }}
                <div class="row">
                    <div class="col-md-6">{{ form.food_name.label }} </div>
                    <div class="col-md-6">{{  form.food_name() }} {% for error in form.food_name.errors %} <div class="flash">{{ message }} </div>{% endfor %}</div>
                </div>
                <div class="row">
                    <div class="col-md-6">{{ form.food_description.label }} </div>
                    <div class="col-md-6">{{  form.food_description() }} {% for error in form.food_description.errors %} <div class="flash">{{ message }}</div>{% endfor %}</div>
                </div>
                **<div class="row">
                    <div class="col-md-6">{{ form.event_starts.label }} </div>
                      <div class="col-md-6"><input id="event_starts" type="datetime-local" value="2014-10-31T00:00:01"> {% for error in form.event_starts.errors %} <div class="flash">{{ message }}</div>{% endfor %}</div>
                </div>**


 <div class="row">
                <div class="col-md-6">{{ form.event_ends.label }} </div>
                <div class="col-md-6"><input id="event_ends" type="datetime-local" value="2014-10-31T00:00:01"> {% for error in form.event_ends.errors %} <div class="flash">{{ message }}</div>{% endfor %}</div>
            </div>
                <div class="row">
                    <div class="col-md-6">{{ form.price.label }} </div>
                    <div class="col-md-6">{{  form.price() }} {% for error in form.price.errors %} <div class="flash">{{ message }}</div>{% endfor %}</div>
                </div>
                <div class="row">
                    <div class="col-md-6">{{ form.cuisine_type.label }} </div>
                    <div class="col-sm-2 control-label" for="formGroupInputLarge">{{  form.cuisine_type() }} {% for error in form.cuisine_type.errors %} <div class="flash">{{ message }}</div>{% endfor %}</div>
                </div>
                {% with messages = get_flashed_messages() %}
        {% if messages %}
        <ul class=flashes>
        {% for message in messages %}
            <li>{{ message | safe }}</li>
        {% endfor %}
        </ul>
      {% endif %}

    {% endwith %}


            <button type="submit" class="btn btn-primary btn-lg" value="submit">Submit</button>

        </form>

在表单和jinja_template中查看event_start日期和event_end日期。 但由于某种原因,form.validate_on_submit()始终为false?

1 个答案:

答案 0 :(得分:1)

我假设您使用value s:idevent_starts来获取两个输入字段的event_ends属性的日期和时间。如果是这样,您应该使用format中的DateTimeField参数将日期时间的格式设置为与模板中的格式相同:

event_starts = DateTimeField('Event Starts',format ='%Y-%m-%dT%H:%M:%S', validators=[Required()])

event_ends = DateTimeField('Event Ends', format ='%Y-%m-%dT%H:%M:%S',validators=[Required()])