django:TypeError:'tuple'对象不可调用

Posted

技术标签:

【中文标题】django:TypeError:\'tuple\'对象不可调用【英文标题】:django: TypeError: 'tuple' object is not callabledjango:TypeError:'tuple'对象不可调用 【发布时间】:2013-08-27 04:56:59 【问题描述】:

出现类型错误,“元组”对象不可调用。知道它可能是什么吗? (不要担心缩进。它会奇怪地复制。)我正在尝试根据 storeliquor 的 PackSize 创建选择。

Views.py:

def storeliquor(request, store_id, liquor_id):        
a = StoreLiquor.objects.get(StoreLiquorID=liquor_id)
s = Store.objects.get(StoreID=store_id)
x = Order.objects.get(storeID=s, Active=True)
y = a.OffPremisePrice
c = a.BottleSize

g = request.POST.get('OrderAmount', '')
b = a.PackSize
h = b*2
d = b*3
e = b*4
r = b*5
if c == "1750 ML":
    pack_size = (
        ('1', '1')
        ('3', '3')
        (b, b)
        (h, h)
        (d, d)
        (e, e)
        (r, r)
    )
elif c == "1000 ML":
    pack_size = (
        ('1', '1')
        ('3', '3')
        ('6', '6')
        (b, b)
        (h, h)
        (d, d)
        (e, e)
        (r, r)
    )
elif c == "750 ML":
    pack_size = (
        ('1', '1')
        ('3', '3')
        ('6', '6')
        (b, b)
        (h, h)
        (c, d)
        (e, e)
        (r, r)
    )     
elif c == "375 ML":
    pack_size = (
        ('3', '3')
        ('6', '6')
        ('12', '12')
        (b, b)
        (h, h)
        (d, d)
        (e, e)
        (r, r)
    )        
elif c == "200 ML":
    pack_size = (
        ('12', '24')
        ('24', '24')
        (b, b)
        (c, c)
        (c, d)
        (e, e)
        (r, r)
    ) 
else:
    pack_size = (
        (b, b)
        (c, c)
        (c, d)
        (e, e)
        (r, r)
    )        




if request.method == "POST":
    f = AddToOrderForm(request.POST)

    if f.is_valid():
        z = f.save(commit=False)
        z.TotalPrice = (float(y)) * (float(g))
        z.storeliquorID = a
        z.orderID = x

        z.save()        

    return HttpResponseRedirect('/stores/get/%s' % store_id)

else:
    f = AddToOrderForm()
    f.fields['OrderAmount'].choices = pack_size      
args = 


args['liquor'] = a
args['s'] = s
args['form'] = f   

return render(request,'storeliquor.html', args)

模型文件:

class LiquorOrder(models.Model):

LiquorOrderID = models.AutoField(primary_key=True)
storeliquorID = models.ForeignKey(StoreLiquor)
orderID = models.ForeignKey(Order)
OrderAmount = models.CharField('Order Amount', max_length=3)
TotalPrice = models.DecimalField('Total Price', max_digits=5, decimal_places=2)
StorePrice = models.DecimalField('Store Price', max_digits=5, decimal_places=2)

表格文件:

class AddToOrderForm(forms.ModelForm):

class Meta:
    model = LiquorOrder
    fields = ('OrderAmount', 'StorePrice')

【问题讨论】:

pack_size里面的元组应该用逗号隔开。(,) 【参考方案1】:

您的元组中缺少逗号。

在元组之间插入逗号 如图:

pack_size = (('1', '1'),('3', '3'),(b, b),(h, h),(d, d), (e, e),(r, r))

对所有人都这样做

【讨论】:

【参考方案2】:

中间缺少逗号 (,):

>>> ((1,2) (2,3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable

加逗号:

>>> ((1,2), (2,3))
((1, 2), (2, 3))

【讨论】:

以上是关于django:TypeError:'tuple'对象不可调用的主要内容,如果未能解决你的问题,请参考以下文章

django错误笔记——TypeError: view must be a callable or a list/tuple in the case of include().解决办法

Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of includ

TypeError: view must be a callable or a list/tuple in the case of include()

Django 保存到 DB:TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“元组”

TypeError: isinstance() arg 2 must be a type or tuple of types

TypeError:“str”对象的描述符“encode”不适用于“tuple”对象