Py学习  »  问与答

form初始化ChoiceField后跳转模板显示报错:too many values to unpack

_雪山飞狐_ • 7 年前 • 1854 次点击  

大家好,本人小白,刚开始用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>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1691
 
1854 次点击  
文章 [ 3 ]  |  最新文章 7 年前
_雪山飞狐_
Reply   •   1 楼
_雪山飞狐_    7 年前

找到问题了,form的init()函数被重写,我在函数中仍使用未传递参数的方法实例化,所以参数数量不对。

if req.method == 'POST':
        form = StatusForm(req.POST)
_雪山飞狐_
Reply   •   2 楼
_雪山飞狐_    7 年前

@chengx2000 我的getMo是如下定义的,页面初始化时已经正常显示了下拉。 ` def getMo(dbname,machineId): cursor = connections[dbname].cursor() sqlstr = ''' SELECT TOP 5 ...... ''' cursor.execute(sqlstr,[machineId]) results = namedtuplefetchall(cursor) #print results molist = [] molist.append(('','------------------')) for result in results:

            rr2 = result.MONO+" || "+result.WORKSHIFT.strip()
            tup = (result.MONO,rr2)
            molist.append(tup)
    return molist

而且console打印的results如下,也是2个元组的list:[('', '------------------'), (u'5161-20161015001', u'5161-20161015001 || A || 52 00 PCS '), (u'5161-20161004001', u'5161-20161004001 || A || 26900 PCS '), (u'516 1-20160929001', u'5161-20160929001 || A || 1700 PCS '), (u'5161-20160916006', u' 5161-20160916006 || A || 150 PCS '), (u'5161-20160901001', u'5161-20160901001 || a || 1100 PCS ')]`

views.py也已正常返回到html页面,在页面显示的时候报的错。 return render_to_response('changestatus.html',{'form':form,'machineId':machineId})

chengx2000
Reply   •   3 楼
chengx2000    7 年前

你的信息量太少,就“too many values to unpack”这一条我在stackoverflow找到一条解释。 先看下面的代码:

def returnATupleWithThreeValues():
    return (1,2,3)
a,b,c = returnATupleWithThreeValues()
print a
print b
print c

但是如果你这样写:

def returnATupleWithThreeValues():
    return (1,2,3)
a,b = returnATupleWithThreeValues()
print a
print b

就会出错:

Traceback (most recent call last):
  File "c.py", line 3, in ?
    a,b = returnATupleWithThreeValues()
ValueError: too many values to unpack

你自己看看究竟哪里出错了。