Opened 12 years ago
Last modified 12 years ago
#19044 closed Cleanup/optimization
DeletionMixin: allow substitution in success_url — at Initial Version
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Generic views | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Let's say I have object with parent set and once object is deleted, browser should be redirected to parent page.
Expected way to work is:
url(r'object/(?P<pk>\d+)/delete/$', DeleteView.as_view(model=Object, success_url='/parent/%(parent_id)d/'), name='object_delete'),
This can be achieved by evaluating success_url first and then deleting object:
class DeletionMixin(object):
"""
A mixin providing the ability to delete objects
"""
success_url = None
def delete(self, request, *args, kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
self.object.delete()
return HttpResponseRedirect(success_url)
# Add support for browsers which only accept GET and POST for now.
def post(self, *args, kwargs):
return self.delete(*args, kwargs)
def get_success_url(self):
if self.success_url:
return self.success_url % self.object.dict
else:
raise ImproperlyConfigured(
"No URL to redirect to. Provide a success_url.")