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))
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
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
精彩评论