Login

Return to change_list with filter after change

Author:
graveyboat
Posted:
August 14, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

This snippet allows you to return back to the filtered change_list after clicking "Save" on a change form. Other snippets I've found don't seem to take into account clicking on "Save and add another" or "Save and continue"

 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.