Opened 12 years ago

Last modified 12 years ago

#19044 closed Cleanup/optimization

Allow get_success_url to access self. object in DeletionMixin — at Version 9

Reported by: anonymous Owned by: Simon Charette
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 (last modified by Simon Charette)

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.")

Change History (11)

comment:1 by Preston Holmes, 12 years ago

Easy pickings: set
Needs documentation: set
Needs tests: set
Summary: DeletionMixin: allow substitution in success_urlAllow get_success_url to access self. object in DeletionMixin
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization
Version: 1.4master

To simplify and clarify a bit:

the get_success_url should be able to see self.object when it is called - and so should happen before the object is deleted.

Seems completely reasonable, and should be backwards compatible.

see https://docs.djangoproject.com/en/dev/internals/contributing/ regarding patch format requirements

by Nicolas Valcárcel, 12 years ago

Attachment: 19044.diff added

comment:2 by Nicolas Valcárcel, 12 years ago

Owner: changed from nobody to Nicolas Valcárcel
Status: newassigned

comment:3 by Nicolas Valcárcel, 12 years ago

Needs tests: unset

comment:4 by Nicolas Valcárcel, 12 years ago

Needs documentation: unset

comment:5 by anonymous, 12 years ago

Attached patch doesn't address an issue properly.
Please take a look at pull request - https://github.com/django/django/pull/417

comment:6 by Preston Holmes, 12 years ago

Needs tests: set
Patch needs improvement: set

I have no idea why the % dict pattern was chosen - I'd prefer to make the code consistent for now - but not overtly document it.

pull is more complete than the attached patch - not sure if it is the same author - but if not please note the convention of letting one person work on a feature at a time.

Tests are needed - committer could take care of a brief release note


comment:7 by Nick Sandford, 12 years ago

Needs tests: unset
Patch needs improvement: unset

Added tests, pull request here: https://github.com/django/django/pull/663/.

comment:8 by Marc Tamlyn, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Simon Charette, 12 years ago

Description: modified (diff)
Owner: changed from Nicolas Valcárcel to Simon Charette

Assigning to myself since I'm planning to commit this.

Note: See TracTickets for help on using tickets.
Back to Top