Redirect to Custom URL After Saving in Django Admin

July 27, 2012

Here’s a quick and dirty admin hack that can save you lots of time if you’re used to browsing between editing objects in admin and vieving the results on a live site.

Put following change_view-method in your and now you can make links to admin change page that return you back to where you came from:

class BlogEntryAdmin(admin.ModelAdmin):


    def change_view(self, request, object_id, form_url='', extra_context=None):
        result = super(BlogEntryAdmin, self).change_view(request, object_id, form_url, extra_context)
        if request.GET.get('return_to', False):            
            result['Location'] = request.GET['return_to']
        return result

In your template you’d have something like:

{% if request.user.is_staff %}
    <a href="/admin/blog/blogentry/{{ }}/?return_to={{ entry.get_absolute_url }}">Edit in admin</a>
{% endif %}

Of course, if you’re smart, you should convert any places that need this kind of functionality with frontend editing tools so you can forget going to the Admin in the first place. But meanwhile, those few lines of code may come in handy!

Tagged with , ,
Proudly Powered by Django © 2000-2019 Ville Säävuori. All lefts reversed.