[求助贴]如何使用配置文件+wtforms动态创建表单

flask-wtf

#1

内容较多,有劳了~
目的:
想通过读取配置文件里面的数据动态配置<阶段>和每个阶段下<任务>,用来统计自己每天各个阶段任务的完成度。

简易原型图:

我写的代码:

class TaskProgressForm(FlaskForm):
      name1 = StringField('今日进度', validators=[DataRequired(), Length(2, 20)])
      name2 = StringField('总进度', validators=[DataRequired(), Length(2, 20)])


class TaskForm(FlaskForm):
    pass
    # mobile_phone = FormField(TaskProgressForm)
    # office_phone = FormField(TaskProgressForm)


class TaskSubmitForm(FlaskForm):
    name = StringField('姓名', validators=[DataRequired(), Length(2, 20)])
    # submit = SubmitField('提交')


@say.route('/task', methods=['GET','POST'])
def task_submit():
    tasks = [
        {'id':'1',
         'name':'准备阶段',
         'content':[
             {'id':'1','name':'任务一'},
			 {'id':'2','name':'任务二'}
         ]
        },{'id':'2',
         'name':'开发阶段',
         'content':[
             {'id':'3','name':'任务三'}
         ]
        },{'id':'3',
         'name':'开发阶段',
         'content':[
             {'id':'4','name':'任务四'}
         ]
        }
    ]
    for item in tasks:
        setattr(TaskSubmitForm, 'form-' + str('id_'+item['id']), FormField(TaskForm,label=item['name']))
        for itm in item['content']:
            setattr(TaskForm, 'form-' + str('id_'+item['id']+'_'+itm['id']), FormField(TaskProgressForm,label=itm['name']))
    form = TaskSubmitForm()
    return render_template('say/task_submit.html', form=form,tasks = tasks)

出现的问题:
每个阶段下面都会出现所有的任务(包括阶段一,二和三节点下的所有任务),烦请大家指点思路~