正在尝试允许用户编辑十进制值“amount”。当用户单击“编辑”时,将显示一个十进制字段。当用户完成更改值并单击“完成”。。。你知道吗
它抛出以下错误:
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/'))
我做不到。帮一个笨蛋?你知道吗
正如上面的评论所说,表单没有发布任何名为“id”的字段。您想要的是“编辑金额{{id}”。你知道吗
下面是一个如何从表单字段名中提取ID的示例。它缺少exception catching和其他一些你需要注意的“问题”。但这是基本的想法。你知道吗
我建议您添加一个带有ID的hidden form field,而不是这样做。您还应该检查用户的check the permissions,并确保他们能够修改该特定记录。你知道吗
相关问题 更多 >
编程相关推荐