#19044: Allow get_success_url to access self. object in DeletionMixin
-------------------------------------+-------------------------------------
     Reporter:  anonymous            |                    Owner:  charettes
         Type:                       |                   Status:  assigned
  Cleanup/optimization               |                  Version:  master
    Component:  Generic views        |               Resolution:
     Severity:  Normal               |             Triage Stage:  Ready for
     Keywords:                       |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by charettes):

 * owner:  nxvl => charettes


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

New 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:
 {{{
 #!python
 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:
 {{{
 #!python
 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.")
 }}}

--

Comment:

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

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19044#comment:9>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to