【发布时间】:2012-03-29 04:23:51
【问题描述】:
试图允许用户编辑十进制值“金额”。当用户单击“编辑”时,会显示一个小数字段。当用户完成更改值并单击“完成”时...
它会抛出这个错误:
Exception Type: ValueError
Exception Value: Cannot use None as a query value
这是我的模板:
<form class="nice input-prepend edit-allocation-form" action="/portfolio/edit/" method="POST">
{% csrf_token %}
<input class="input-text required number" type="text" name="edit_amount{{ investment.position.id }}" {% if investment.position.amount %}value="{{ investment.position.amount }}"{% endif %}>
<a href="#" class="edit" id="edit{{ investment.position.id }}">edit</a>
<button class="hide" type="submit" id="saveAmountButton"></button>
</form>
这是我的观点:
@login_required
def edit(request):
if request.method == 'POST':
profile = get_object_or_404(InvestorProfile, user=request.user)
position = InvestorPosition.objects.filter(id__in=request.POST.get('id'))
if Decimal(request.POST['edit_amount' + str(position.id)]) != position.amount:
position.amount = Decimal(request.POST['edit_amount' + str(position.id)])
position.save()
return HttpResponseRedirect(request.POST.get('next', '/portfolio/'))
我无法完成这项工作。帮助菜鸟?
【问题讨论】:
-
request.POST.get('id')是否返回某些内容(不是None)?我没有看到您的表单在某处设置了id字段? -
您应该将其发布为答案,因为它是正确的。除非他有一些他决定不显示的 JS,否则 id 永远不会被发布,所以它当然会返回 None
-
我还想添加此代码还有许多其他问题。使用
-
使用 没有任何问题。它比 更容易使用。它看起来确实像操作隐藏按钮并做一些javascript技巧以提交表单。这是个坏主意。
-
@nijansen