Django:String对象打印出无

时间:2011-02-28 10:10:08

标签: python django string views

您好我有一个删除订单号的视图。删除字符串后,网页将重定向到客户端页面,让用户知道订单号已被删除。这儿存在一个问题。我得到了这个。

Previous order None deleted

它说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))

2 个答案:

答案 0 :(得分:0)

delete()对模型实例执行的操作之一是将其pk设置为None,因此如果您尝试保存它,它将创建一个新实例,而不是尝试更新旧的(不再存在的)。

所以,简单的解决方案:在删除之前抓住PK。事实上,你可以摆脱对你所拥有的delete的第一次调用,因为你在创建消息后再次调用它。

顺便说一下,创建消息的更好方法是使用字符串格式:

message = "Previous order %s deleted" % order_number.pk

答案 1 :(得分:0)

非常粗心的错误。为什么我写了两次order_number.delete()

if request.POST.get('delete'):
    order_number.delete() # Why was this there? of course, if I have this it will delete till it gets to the next part where it informs the user which order number was deleted! 
    request.user.message_set.create(message = "Previous order " + str(order_number.pk) + "  deleted")
    order_number.delete()

已修复