Same Con to Hoyci — too many choices —

A blog about Web development, usability and AI.

Redirect to Custom URL After Saving in Django Admin

 by Ville Säävuori

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 admin.py 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/{{ entry.pk }}/?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!

Follow @uninen on Twitter