Return to change_list with filter after change

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class ReturnAdmin(admin.ModelAdmin):
    """
    Redirects user back to filtered list after changing an object and
    clicking Save.  Simply add:
    {% if HTTP_REFERER %}<input type="hidden" name="HTTP_REFERER" value="{{ 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("../")

More like this

  1. keeping filter states after edits by chronosllc 2 years, 7 months ago
  2. Admin: return to change_list with filter and pagination applied by fx 3 years ago
  3. Keeping filter states after edits (Django 1.4) by rhfung 1 year, 4 months ago
  4. Return to a filtered changelist on change form save by richardbolt 3 years, 8 months ago
  5. Drag and drop ordering of admin list elements for Grappelli [v2] by brejoc 1 year, 3 months ago

Comments

(Forgotten your password?)