In my code, I email the user asking whether they want to confirm or deny the order by sending them two links (make the bool false or true). The only problem is, they can continually change the bool by clicking true then false true then false, etc. I would like it to where if they click the true link, they can't go back and make it false.
Here's the code in views.py:
def confirm(request, itemnum):
item = get_object_or_404(PurchaseOrder, item_number = itemnum)
item.confirmed = True
item.save()
confirm_title = 'Purchase Order %s Confirmed' % item.product
send_mail(confirm_title, 'Check the Product Order System to see the updated list.', 'MyEmail@gmail.com',['YourEmail@gmail.com'], fail_silently=False)
return HttpResponse('Product %s confirmed' % item.product )
def deny(request, itemnum):
item = get_object_or_404 (PurchaseOrder, item_number = itemnum)
item.confirmed = False
item.save()
deny_title = 'Purchase Order %s Denied' % item.product
send_mail(deny_title, 'Check the Product Order System to see the updated list.', 'MyEmail.com', ['YourEmail@gmail.com'], fail_silently = False)
return HttpResponse('Product %s denied' % item.product)
I would handle this using a NullBooleanField
, initialize the value to None
before confirming or denying, then detect non- None
values in the confirm and deny views and provide whatever response you want to give on attempts to change the status. I would probably return a page saying something like "Product %s has already been confirmed" or the like depending on its status, but obviously it depends on your situation.
Other solutions are certainly possible - a foreign key to a Status model could be a good idea if you anticipate getting any more complicated.
An alternative to Peter's answer is to have two boolean fields, the first being the one you're dealing with now, and the second called HasResponded
. Then when the user clicks the link, the view does the following:
if instance.HasResponded:
return render(request, 'already-responded.html',)
else:
instance.HasResponded = True;
instance.Bool = response
instance.save()
return render(request, 'template.html',)
Just to give you some choice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.