class ReturnAdmin(admin.ModelAdmin): """ Redirects user back to filtered list after changing an object and clicking Save. Simply add: {% if HTTP_REFERER %}{% endif %} to your change_form.html and use this instead of admin.ModelAdmin in your admin.py """" def change_view(self, request, object_id, extra_context={}, *args, **kwargs): if 'HTTP_REFERER' in request.META: extra_context['HTTP_REFERER'] = str(request.META['HTTP_REFERER']) return super(ReturnAdmin, self).change_view(request, object_id, extra_context, *args, **kwargs) def response_change(self, request, obj): """ Determines the HttpResponse for the change_view stage. """ opts = obj._meta pk_value = obj._get_pk_val() msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)} if request.POST.has_key("_continue"): self.message_user(request, msg + ' ' + _("You may edit it again below.")) if request.REQUEST.has_key('_popup'): return HttpResponseRedirect(request.path + "?_popup=1") else: return HttpResponseRedirect(request.path) elif request.POST.has_key("_saveasnew"): msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj} self.message_user(request, msg) return HttpResponseRedirect("../%s/" % pk_value) elif request.POST.has_key("_addanother"): self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name))) return HttpResponseRedirect("../add/") elif request.POST.has_key("HTTP_REFERER"): self.message_user(request, msg) return HttpResponseRedirect(request.POST['HTTP_REFERER']) else: self.message_user(request, msg) return HttpResponseRedirect("../")