Django:字符串对象打印出 None
您好,我有一个删除订单号的视图。删除字符串后,网页会重定向到客户端页面,让用户知道订单号已删除。有问题。我明白了。
上一个订单未删除
它显示无
,但我希望它注明我已删除的订单号。似乎没有显示,因为我已经删除了该订单号,所以当它在订单号列表中查找时,找不到该号码。
编辑:这是解决方案。订单号应在消息发送后删除。
def delete_confirmation(request, order_no = 0, service_type = 0):
order = None
count = 0
title = models.SERVICE_CHOICES[int(service_type) - 1][1]
#title = type[1]
order_number = request.session['order_number']
try:
order = models.Order.objects.get(pk = order_no)
count = order.orderservicelist_set.count()
if request.POST.get('delete'):
request.user.message_set.create(message = "Previous order " + str(order_number.pk) + " deleted")
order_number.delete()
return HttpResponseRedirect(reverse(return_clients))
except:
return HttpResponseRedirect(reverse(return_clients))
return render_to_response('delete_order.html', {'order':order, 'title':title, 'count':count, 'order_no':order_no }, context_instance = RequestContext(request))
Hello I have a view that deletes order numbers. Once a string has been deleted, the web page redirects to the clients page letting the user know that the order number was deleted. There is a problem. I am getting this.
Previous order None deleted
It says None
but I want it to state the order number that I have deleted. It seems it says none because I have already deleted that order number so when it looks in the order number list, it cannot find that number.
EDIT: here is the solution. Order number should be deleted after the message is sent.
def delete_confirmation(request, order_no = 0, service_type = 0):
order = None
count = 0
title = models.SERVICE_CHOICES[int(service_type) - 1][1]
#title = type[1]
order_number = request.session['order_number']
try:
order = models.Order.objects.get(pk = order_no)
count = order.orderservicelist_set.count()
if request.POST.get('delete'):
request.user.message_set.create(message = "Previous order " + str(order_number.pk) + " deleted")
order_number.delete()
return HttpResponseRedirect(reverse(return_clients))
except:
return HttpResponseRedirect(reverse(return_clients))
return render_to_response('delete_order.html', {'order':order, 'title':title, 'count':count, 'order_no':order_no }, context_instance = RequestContext(request))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
delete()
对模型实例执行的操作之一是将其 pk 设置为None
,这样,如果您尝试保存它,它将创建一个新实例,而不是而不是尝试更新旧的(不再存在的)。所以,简单的解决方案:在删除之前获取 PK。事实上,您可以删除对
delete
的第一次调用,因为您在创建消息后再次调用它。顺便说一句,创建消息的更好方法是使用字符串格式:
One of the things that
delete()
does on a model instance is set its pk toNone
, so that if you try and save it, it will create a new instance rather than trying to update the old (no-longer-existing) one.So, easy solution: grab the PK before you delete. In fact, you could just get rid of the first call to
delete
you have there, because you call it again after creating the message.By the way, a better way of creating the message is to use string formatting:
非常粗心的错误。为什么我的
order_number.delete()
写了两次?固定的
Very Careless mistake. why have I got
order_number.delete()
written twice?fixed