flask wtform动态变化--增加/删除

两种方法

  1. 前端js写代码调整
  2. 后端改写python代码

1. 前端js写代码调整

github资源
我没验证过

2. 后端改写python代码

注意, 这样子写不如前端修改灵活

class OneStuForm(FlaskForm):

    name = StringField('姓名', validators=[DataRequired()])
    stunum = StringField('学号',validators=[DataRequired(), Length(6,6)] )
    phone = StringField('手机号',validators=[DataRequired(),Length(11,11)])
    email = StringField('电子邮箱地址', validators=[
                        DataRequired(), Length(1, 64), Email()])
    college = StringField('学院', validators=[DataRequired()])
    major = StringField('专业', validators=[DataRequired()])



def signupcompetition():
    
    members = 2


    class LocalForm(FlaskForm): pass
    
    for i in range(0, members):
        if i==0:
            setattr(LocalForm, '组长',FormField(OneStuForm, label='组长'))
        else:
            setattr(LocalForm, '组员'+str(i), FormField(OneStuForm,label='组员'+str(i)))
    LocalForm.submit =  SubmitField('提交报名信息')
    form = LocalForm()

    return  render_template('competitionregister.html', form=form)

你可能感兴趣的:(python,数据库相关)