大家好,本人小白,刚开始用django开发web应用。目前遇到一个问题,在使用外部数据库数据查询结果初始化为form的一个ChoiceField的choices后,提交数据报错。请各位大神帮忙看下是什么问题,已经困扰我好久了。
forms.py
MACHINE_STATUS = (
('OK','OK'),
('NG','NG'),
('STOP','STOP'),
)
class StatusForm(forms.Form):
machineStatus = forms.ChoiceField(label='MACHINE_STATUS',widget=forms.RadioSelect(),choices=MACHINE_STATUS)
def __init__(self,molist,*args,**kwargs):
super(StatusForm,self).__init__(*args,**kwargs)
self.fields['mo'] = forms.ChoiceField(label='MO',choices=molist)
views.py
if req.method == 'POST':
#do somthing
else:
form = StatusForm(molist=getMo('xxxxdb',machineId)) #getMo()从指定数据库查询结果并返回类似[('a','a'),('b','b'),]的列表,我已经把数据库结果集限定为查询TOP 5
return render_to_response('changestatus.html',{'form':form,'machineId':machineId})
页面初始化显示没问题,可以显示数据库查询的结果并生成select。但是提交表单之后报错:
(贴不了图片???)
Error during template rendering
In template E:\Python\Workspace\hzcy_web\board\templates\changestatus.html, error at line 6
too many values to unpack
1 <h2>The machine is: {{machineId}}</h2>
2
3 {{value|time}}
4
5 <form method='post'>
6 {{form.as_p}}
7 <input type='submit' value='OK'/>
8 </form>