chudley
chudley

Reputation: 33

Django DeleteView redirect to variable location

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:

Upvotes: 0

Views: 698

Answers (1)

sean2000
sean2000

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

Related Questions