Flask表单 - Fieldlist例子

样例1

表单类

class FilterForm(wtf.Form):
    filter_value = wtf.TextField('Value', validators=[validators.Required()])
    filter_operator = wtf.SelectField('Operator', validators=[validators.Required()])
    filter_compare_value=wtf.TextField('Compare Value', validators=[validators.Required()])


class RedirectForm(wtf.Form):
    redirect_id = wtf.HiddenField('id')
    redirect_name = wtf.TextField('Name', validators=[validators.Required()])
    redirect_url = wtf.TextField('URL', validators=[validators.Required()])
    redirect_type = wtf.SelectField('Type', validators=[validators.Required()])
    redirect_method = wtf.SelectField('Method', validators=[validators.Required()])
    redirect_active = wtf.BooleanField('Is Active')
    redirect_filters_any = wtf.FieldList(wtf.FormField(FilterForm))
    redirect_filters_all = wtf.FieldList(wtf.FormField(FilterForm))

模板渲染

{% extends "base.html" %}
{% set active_page = "endpoints" %}
{% block tail_script %}

{% endblock %}
{% block content %}
General {{ form.hidden_tag() }}
{{ form.redirect_name.label }}
{{ form.redirect_name|safe }} {% if form.redirect_name.errors %}
    {% for error in form.redirect_name.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ form.redirect_type.label }}
{{ form.redirect_type|safe }} {% if form.redirect_type.errors %}
    {% for error in form.redirect_type.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ form.redirect_active.label }}
{{ form.redirect_active|safe }} {% if form.redirect_active.errors %}
    {% for error in form.redirect_active.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ form.redirect_method.label }}
{{ form.redirect_method|safe }} {% if form.redirect_method.errors %}
    {% for error in form.redirect_method.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ form.redirect_url.label }}
{{ form.redirect_url|safe }} {% if form.redirect_url.errors %}
    {% for error in form.redirect_url.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
Meet All Filters Add {% for f in form.redirect_filters_all %} {% endfor %}
{{ f.filter_value }} {% if f.filter_value.errors %}
    {% for error in f.filter_value.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ f.filter_operator }} {% if f.filter_operator.errors %}
    {% for error in f.filter_operator.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ f.filter_compare_value }} {% if f.filter_compare_value.errors %}
    {% for error in f.filter_compare_value.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
Remove
Meet Any Filters Add {% for f in form.redirect_filters_any %} {% endfor %}
{{ f.filter_value }} {% if f.filter_value.errors %}
    {% for error in f.filter_value.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ f.filter_operator }} {% if f.filter_operator.errors %}
    {% for error in f.filter_operator.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
{{ f.filter_compare_value }} {% if f.filter_compare_value.errors %}
    {% for error in f.filter_compare_value.errors %}
  • {{ error }}
  • {% endfor %}
{% endif %}
Remove
{% if g.user.user_type == 'admin' %} {% endif %}
{% endblock %}

样例2

forms.py

class WarehouseTicketItemForm(Form):
      description = StringField(u'Description', validators=[Required()])
      quantity = IntegerField(u'Quantity', validators=[Required()])

class WarehouseTicketForm(Form):
      spot_id = SelectField(u'Spot', coerce=int)
      items = FieldList(FormField(WarehouseTicketItemForm), min_entries = 3)

warehouse_ticket_form.html

{{ form.hidden_tag() }} {{ form.spot_id }}
{% for item in form.items %} {{ item }} {% endfor %}

warehouse_tickets.py

@coordinator.route('/warehouses/tickets/update', methods=['GET', 'POST'])
def update_warehouses_tickets():
    page = dict(title="Warehouse Ticket")
    form = WarehouseTicketForm()
    form.spot_id.choices = [(s.id, s.name) for s in Spot.query.filter_by(status=True)]
    if form.validate_on_submit():
        print form.data
    return render_template('coordinators/warehouses/warehouse_ticket_form.html',
                        page=page, form=form)

你可能感兴趣的:(Flask表单 - Fieldlist例子)