Django - Defining ?next page from Admin

47 views Asked by At

Is it possible to direct a user back to the previous page coming from the admin dashboard?

I have an "edit" button, and since the "users" who will be able to edit data are savvy enough, I'd rather just have this direct to a change page in admin.

Here is my link:

<td><a href="/admin/inv/lifesafety/{{life.pk}}/change/?next=/stores/{{store.pk}}" target="_blank"><i class="icon ion-md-create"></i></a></td>

Use of ?next= from admin doesn't seem to work. For now I'm just having it opened in a different tab, but I'd like to at least know if there is a direct restriction, or if there's more to it when interacting with admin.

Here's an example:

  • User goes to /stores/1617/
  • User clicks "Edit"
  • "Edit" directs them to /admin/inv/lifesafety/1617/change/?next=/stores/1617
  • After User submits form, User is directed back to /stores/1617
1

There are 1 answers

4
schillingt On BEST ANSWER

There's no built-in way to do this, but you can accomplish it another way by overriding the response_change method of the LifeSafety admin class.

from django.shortcuts import redirect

class LifeSafetyAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        next = request.GET.get('next')
        if next:
            return redirect(next)
        return super().response_change(request, obj)