[求助贴]如何使用配置文件+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)

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


#2

试试 type 动态创建类。举例:

>>> class A:                                                             
...     pass                                                             
...      
>>> for i in range(0, 2):                                                
...     t = type('B', (object,), {'a_{0}'.format(i): 'something value'}) 
...     setattr(A, 'a_{0}'.format(i), t)                                 
...     for j in range(0, 2):                                            
...         setattr(t, 't_{0}_{1}'.format(i, j), 'something value')      
...                                                                      
>>> a = A()                                                              
>>> a                                                                    
<__main__.A object at 0x0000026399AAFD68>                                
>>> dir(A)                                                               
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init
__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__
new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
of__', '__str__', '__subclasshook__', '__weakref__', 'a_0', 'a_1']       
>>> a.a_0                                                                
<class '__main__.B'>                                                     
>>> a.a_1                                                                
<class '__main__.B'>                                                     
>>> dir(a.a_0)                                                           
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init
__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__
new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
of__', '__str__', '__subclasshook__', '__weakref__', 'a_0', 't_0_0', 't_0
_1']                                                                     
>>> dir(a.a_1)                                                           
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init
__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__
new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
of__', '__str__', '__subclasshook__', '__weakref__', 'a_1', 't_1_0', 't_1
_1']                                                                     
>>>