【问题标题】:WTFForms totally automatize form generationWTForms 全自动表单生成
【发布时间】:2020-10-28 14:43:49
【问题描述】:

我有以下表格,使用基本的 HTML 生成:

我现在正在尝试将 WTF Forms 插件与 Flask 一起使用。现在我这样做是为了生成我的输入。我的问题是我不知道如何使用每个输入的类创建引导行。 (例如:输入类是 col-md-12,因此只使用此输入创建一行。如果以下输入类是 col-md-6,则为多个输入创建一行)。

class SupplierForm(Form):
    supplierInfo_name = StringField(
        lazy_gettext('NAME'),
        [validators.required()],
        render_kw={
            'class': 'col-md-12 form-control',
            'onfocus': "searchSupplier()",
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        },
    )
    supplierInfo_address = StringField(
        lazy_gettext('ADDRESS'),
        [validators.required()],
        render_kw={
            'class': 'col-md-12 form-control',
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        }
    )
    supplierInfo_postal_code = StringField(
        lazy_gettext('ZIP_CODE'),
        [validators.required()],
        render_kw={
            'class': 'col-md-6 form-control',
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        }
    )
    supplierInfo_city = StringField(
        lazy_gettext('CITY'),
        [validators.required()],
        render_kw={
            'class': 'col-md-6 form-control',
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        }
    )
    supplierInfo_vat_number = StringField(
        lazy_gettext('VAT_NUMBER'),
        [validators.required()],
        render_kw={
            'class': 'col-md-12 form-control',
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        }
    )
    supplierInfo_siret_number = StringField(
        lazy_gettext('SIRET_NUMBER'),
        [validators.required()],
        render_kw={
            'class': 'col-md-6 form-control',
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        }
    )
    supplierInfo_siren_number = StringField(
        lazy_gettext('SIREN_NUMBER'),
        [validators.required()],
        render_kw={
            'class': 'col-md-6 form-control',
            'onfocusout': "ocrOnFly(true, this); removeRectangle()",
            'onfocusin': "ocrOnFly(false, this)"
        }
    )

我现在的 HTML,所有的输入都是一行一行的显示

{% macro render_field(field) %}
<div class="form-group">
    <label for="{{ field.id }}">
        {{ field.label }}
    </label>
    <div class="input-group mb-2">
        <div onclick="drawRectangle(document.getElementById('{{ field.id }}'))" class="input-group-prepend">
            <div class="input-group-text"><i class="fas fa-eye" aria-hidden="true"></i></div>
        </div>
        {{ field(**kwargs)|safe }}
        {% if field.errors %}
            <ul class=errors>
                {% for error in field.errors %}
                    <li>{{ error }}</li>
                {% endfor %}
            </ul>
        {% endif %}
    </div>
</div>
{% endmacro %}
    
<form method=post>
    {% for field in form %}
        {{ render_field(field) }}
    {% endfor %}
    <p><input type=submit value=Register>
</form>

【问题讨论】:

    标签: python flask flask-wtforms wtforms


    【解决方案1】:

    我终于可以做我想做的事了。我重写了 WTFORMS 的 Fied 类,以将我可以使用的自定义属性添加到自定义 StringField 类中

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 1970-01-01
      • 2021-03-04
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多