Django:字符串对象打印出 None

发布于 2024-10-19 19:27:06 字数 1133 浏览 1 评论 0原文

您好,我有一个删除订单号的视图。删除字符串后,网页会重定向到客户端页面,让用户知道订单号已删除。有问题。我明白了。

上一个订单未删除

它显示,但我希望它注明我已删除的订单号。似乎没有显示,因为我已经删除了该订单号,所以当它在订单号列表中查找时,找不到该号码。

编辑:这是解决方案。订单号应在消息发送后删除。

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-10-26 19:27:06

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

所以,简单的解决方案:在删除之前获取 PK。事实上,您可以删除对 delete 的第一次调用,因为您在创建消息后再次调用它。

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

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

One of the things that delete() does on a model instance is set its pk to None, 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:

message = "Previous order %s deleted" % order_number.pk
゛清羽墨安 2024-10-26 19:27:06

非常粗心的错误。为什么我的 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()

固定的

Very Careless mistake. why have I got order_number.delete() written twice?

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()

fixed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文