On one of my sites i have a bunch of different models for various content resources, e.g. articles, videos and podcasts. For admins I wanted to add an "edit this item" link to the menu when browsing the site. Pretty easy: check permissions and add a link to the admin.
Now, ideally i'd like the user to be redirected back to the main site on
save, instead of to the default django admin list view. After an initial
solution that involved copying the entire django code for response_change
,
I had a much better idea. I was already appending a GET
parameter
?source=main
to my links to the admin to distinguish from the links from
within the admin itself (for which i want the default redirect behaviour).
from django.http import HttpResponseRedirect
class EntryAdmin(admin.ModelAdmin):
def response_change(self, request, obj):
""" if user clicked "edit this page", return back to main site """
response = super(EntryAdmin, self).response_change(request, obj)
if (isinstance(response, HttpResponseRedirect) and
response['location'] == '../' and
request.GET.get('source') == 'main'):
response['location'] = obj.get_absolute_url()
return response
Comments !