Reputation: 501
im using the delete view and i want to customize the action of the deletion.
there are some checks i need to do before deleting the object and in case the
checks doesn't pass i want to redirect to the same view and show a matching
massage (just like i can do in the form_valid method with the form.add_error
and return self.form_invalid(form)).
so i have the following delete method:
def delete(self, request, *args, **kwargs):
if XXXX:
### add_error and return a massage ###
else:
return super(delete, self).delete(request, *args, **kwargs)
what i need/can do in the "if" to prevent the deletion and explain the user
what the problem is?
thx
Upvotes: 1
Views: 3339
Reputation: 1194
To prevent deletion you just have not to call super().delete in the if clause.
For explaining why deletion was not performed you can use django messages (https://docs.djangoproject.com/en/1.8/ref/contrib/messages/)
So I think you can use something like this in your if clause
messages.add_message(request, messages.WARNING, 'Object was not deleted')
return self.render_to_response(self.get_context_data())
And you'll be able to access message text using code from django messages documentation: https://docs.djangoproject.com/en/1.8/ref/contrib/messages/#django.contrib.messages.get_messages
Upvotes: 1