wtforms CRUD-在SelectField中设置默认值 [英] wtforms CRUD - set default in SelectField

查看:77
本文介绍了wtforms CRUD-在SelectField中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CRUD应用程序,其中wtforms被用于填充带有"for"循环的一系列表单,以允许用户编辑数据库中的现有数据.我正在使用"value = row.XXX";默认情况下,使用来自数据库的现有数据来预填充表单.这对于普通的StringFields效果很好,但对SelectField则不起作用.谁能帮忙!?

I have a CRUD application where wtforms is being used to populate a series of forms with a 'for' loop to allow users to edit existing data in the DB. I'm using "value=row.XXX" to pre-populate the form with existing data from the DB as a default. This works well for normal StringFields, but doesn't work for SelectField. Can anyone help!?

下面的html示例.form_edit.group是一个SelectField.令人讨厌的是,当以表格形式显示时,它默认为选择"列表中的第一项,而不是先前选择的数据(value = row.group不适用于StringFields).这意味着,当用户重新提交表单时,它将默认为第一项.

Example html below. The form_edit.group is a SelectField. Annoyingly, when displayed in the form, it defaults to the first item in the 'choices' list rather than the previously chosen data (value=row.group doesn't work as it does for StringFields). This means that when the user comes to resubmit the form it defaults to this first item.

                    <div class="form-group col-sm-6">
                        {{ form_edit.description.label }}
                        {{ form_edit.description(class="form-control", value=row.description) }}
                        {% for error in form_edit.description.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                        {% endfor %}
                    </div>
                    <div class="form-group col-sm-6">
                        {{ form_edit.group.label }}
                        {{ form_edit.group(class="form-control", value=row.group) }}
                        {% for error in form_edit.group.errors %}
                        <span style="color: red;">[{{ error }}]</span>
                        {% endfor %}
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-sm-6">
                        {{ form_edit.qty.label }}
                        {{ form_edit.qty(class="form-control", value=row.qty) }}
                        {% for error in form_edit.qty.errors %}
                        <span style="color: red;">[{{ error }}]</span>

表格:

class Areas_form(FlaskForm):

    hidden_id = HiddenField('Area id')
    description = StringField('Description',validators=[DataRequired()])
    group = SelectField('Group',validators=[DataRequired()],choices=['Ext(Am) Wall','Ext(Gnd) Wall','Roof(Am)','Flr Slab','Flr(Am)'])
    qty = FloatField('Quantity', validators=[DataRequired(message='must be a number')], default=float(1))
    lth_a = FloatField('Length a', validators=[DataRequired(message='must be a number')])
    lth_b = FloatField('Length b', validators=[DataRequired(message='must be a number')])                                 
    assembly = SelectField('Assembly',validators=[DataRequired()], choices=[])
    dev_nth = FloatField('Deviation from North', validators=[InputRequired(message='must be a number')]) 
    ang_hor = FloatField('Angle from horizontal', validators=[InputRequired(message='must be a number')])
    submit = SubmitField('Submit data')

示例行:

当我按编辑"时,出现以下表格.组"指的是组"."Flr Slab"已默认返回选择列表中的第一项-"Ext(Am)Wall".相比之下,说明"指的是描述".字段已将"Floor 1"拉到从数据库中获取.

When I press "edit" the following form comes up. The "group" "Flr Slab", has defaulted back to the first item in the choices list - "Ext(Am) Wall". By contrast the "description" field has pulled "Floor 1" from the database.

推荐答案

class Areas_form(FlaskForm):中更新您的 SelectField 定义如下:

Update your SelectField difinition in class Areas_form(FlaskForm): as follows:

group = SelectField('Group',validators=[DataRequired()],choices=[(1,'Ext(Am) Wall'),(2,'Ext(Gnd) Wall'),(3,'Roof(Am)'),(4,'Flr Slab'),(5,'Flr(Am)')])

我们在其中分配值选项和出现的文本,最后,然后在提交表单后的代码中,将值作为常规列字段获得

where we assign the value option as well as the appeared text, and finally, then in your code after form submission, you get the value as regular column fields

请注意,您将使用数字1,2、3、4或5作为选择中映射的映射,这会更好.

Notice, you will use numbers 1,2,3,4, or 5 as a mapping for your choices in select, which would be better.

提交后添加

为了设置默认值更新

{{ form_edit.group(class="form-control", value=row.group) }}

{{ form_edit.group(class="form-control", value=row.group.data) }}

如果它不起作用,请检查python中 type(row.group.data)的数据类型,如果它是 string 类型,则更新列定义为:

if it does not work, check that data type of type(row.group.data) in python, if it is string type, update the group column difiniation to:

group = SelectField('Group',validators=[DataRequired()],choices=[('1','Ext(Am) Wall'),('2','Ext(Gnd) Wall'),('3','Roof(Am)'),('4','Flr Slab'),('5','Flr(Am)')])

以防万一,我们强制使用整数值,在添加之前,在我的第一个建议的末尾添加 coerce = int ,如下所示:

In case, we force to use integer value, add coerce=int to the end of my first suggestion before additions as follows:

group = SelectField('Group',validators=[DataRequired()],choices=[(1,'Ext(Am) Wall'),(2,'Ext(Gnd) Wall'),(3,'Roof(Am)'),(4,'Flr Slab'),(5,'Flr(Am)'),coerce=int])

寻找您的来信:)

祝你好运

这篇关于wtforms CRUD-在SelectField中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆