如果我设置整数值(product = 1)
,那么它就可以正常工作,而不是id,当我给出id时,响应就是空的。
id = request.POST.get("product")
attrs = ProductAttributes.objects.filter(product=id).values('product', 'size__name')
return JsonResponse(list(attrs), safe=False)
这样:
id = int(request.POST.get("product"))
获取错误:
TypeError at /purchase-order/ajax/load-attrs/
int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Request Method: GET
Request URL: http://127.0.0.1:8000/purchase-order/ajax/load-attrs/
Django Version: 3.1.1
Exception Type: TypeError
Exception Value:
int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Exception Location: D:\MEGA\djangoprojects\myprojects\accpack\views\purchase_order.py, line 76, in load_attrs
Python Executable: C:\Users\Dell\anaconda3\envs\webapp\python.exe
Python Version: 3.7.9
Python Path:
['D:\\MEGA\\djangoprojects\\myprojects',
'C:\\Users\\Dell\\anaconda3\\envs\\webapp\\python37.zip',
'C:\\Users\\Dell\\anaconda3\\envs\\webapp\\DLLs',
'C:\\Users\\Dell\\anaconda3\\envs\\webapp\\lib',
'C:\\Users\\Dell\\anaconda3\\envs\\webapp',
'C:\\Users\\Dell\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\Dell\\anaconda3\\envs\\webapp\\lib\\site-packages',
'C:\\Users\\Dell\\anaconda3\\envs\\webapp\\lib\\site-packages\\astroid\\brain']
发布于 2021-01-03 07:52:13
request.POST.get("product")
将是一个字符串,但可能需要是一个整数。
当这个值等于None
时,你的问题中就会出现错误(例如,它没有作为POST请求的一部分提交)。您应该单独处理此问题。
id_str = request.POST.get("product")
try:
id = int(id_str)
except TypeError:
# Here you will need to decide what to do
# if no id is supplied. Maybe raise Http404?
(查看错误消息。我会进一步注意到,您正在发送一个GET
请求,但是这个视图显然需要一个POST
。也许可以使用require_POST
装饰器。)
https://stackoverflow.com/questions/65545213
复制相似问题