Reputation: 33
I want to be able to use the DeleteView CBV in Django 1.5c1 (including the confirmation page), but have the user be redirected to where he/she clicked the object.
For example, here is a rough outline of my site's structure based around Events:
/events/week/2013/03/ - ListView, shows 3rd week of 2013's Events
/events/month/2013/01/ - ListView, shows January of 2013's Events
/events/year/2013/ - ListView, show 2013's Events
/events/53/ - DetailView, shows a specific Event
On any of these Events listings, I could have an Event that appears on them all. Rather than having an Event's URL depend on the list that the user has navigated from (e.g. /events/year/2013/53/), I've chosen to have the Event be served on an independent URL (e.g. /events/53/).
With that context, I want to be able to have a delete button on my Event's DetailView that redirects back to the ListView that the user navigated from.
I've considered:
?next={{ request.META.HTTP_REFER }}
to the DeleteView's URL and adding it to the delete form somehow, but the whole referrer's URL is passed (e.g. /events/53/delete/?next=www.site.com/events/year/2013/).Upvotes: 0
Views: 698
Reputation: 516
Try something like this as a mixin:
class RedirectURLView(View):
def get_success_url(self):
next_url = self.request.GET.get('next')
if next_url:
return next_url
else:
return super(RedirectURLView, self).get_success_url()
then append ?next={{ request.path }}
to the urls
Upvotes: 1