首页 > 解决方案 > 烧瓶 form.data 值未在提交时更新

问题描述

我有一个表单,可以动态填充使用 .default 方法的值。

像这样:

for i, item in enumerate(staff):
    if item.id == "csrf_token" or item.id == "submitstaff":
        continue
    item.default = stafftable[0][i]
    staff.process()

我也使用了这个表格,没有这个过程,它可以工作。但是,当我在执行此过程后使用它时,它总是采用默认值,不允许用户再更新这些值。例如,如果我显示表单 agian 并尝试更改字段的值,它将采用进程设置的 .default 字段而不是输入。

形式:

class Staff(FlaskForm):
    staffid = StringField(
        validators=[DataRequired(message="Staff Id Field can not be left blank")]
    )
    Firstname = StringField(
        validators=[DataRequired(message="Firstname Field can not be left blank")]
    )
    LastName = StringField(
        validators=[DataRequired(message="LastName Field can not be left blank")]
    )
    ProjectRole = StringField(
        validators=[DataRequired(message="ProjectRole Field can not be left blank")]
    )
    YearsOfExperience = FloatField(
        validators=[
            DataRequired(message="Years of experience Field can not be left blank")
        ]
    )
    Organisation = SelectField(
        "Organisations",
        choices=[
            ("1","1"),
            ("1","1"),
            ("1","1"),
        ],
    )
    OfficeLocation = StringField(
        validators=[DataRequired(message="Office location Field can not be left blank")]
    )
    Discipline = SelectField(
        "Discipline",
        choices=[
            ("Geotechnics", "Geotechnics"),
            ("Civil", "Civl"),
            ("Digital", "Digital"),
            ("Project Management", "Project Management"),
        ],
    )
    KeyPersonName = SelectField(choices=[("No","No"),("Yes","Yes")])
    BatchNo = StringField(
        validators=[DataRequired(message="BatchNo Field can not be left blank")]
    )
    ProjectStatus = StringField(
        validators=[DataRequired(message="ProjectStatus Field can not be left blank")]
    )
    Booking = StringField(
        validators=[DataRequired(message="Booking Field can not be left blank")]
    )
    TasksEarlyDeliverables = TextAreaField(
        validators=[
            DataRequired(
                message="Tasks and early deliverables Field can not be left blank"
            )
        ]
    )

    requester = StringField()
    package = StringField()
    PercentoftimeS1 =  FloatField()
    HourlyRate = FloatField()
    PercentoftimeS2 = FloatField()

    submitstaff = SubmitField(label="Submit", validators=[DataRequired()])

标签: pythonflaskflask-wtformswtformsflask-restful

解决方案


因此,经过一番研究,我发现使用 .process 会导致它无法正常工作。但是,使用上面的代码而不使用 .process 它不会在网页上显示数据。

显示数据的解决方案实际上是使用 .data 而不是 .default。


推荐阅读