### urls.py:
urlpatterns = patterns('',
(r'^admin/(.*?)/(.*?)/edit/(.*?)/$', 'myproj.myapp.views.admin_edit_callback'),
# note -- the pattern for the overall admin must come after
(r'^admin/(.*)', admin.site.root),
)
### views.py:
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.contrib.admin.views.decorators import staff_member_required
CALLBACK_URL_VAR = 'callback_url'
@staff_member_required
def admin_edit_callback(request, app, model, id):
# GET == the initial request -- save the referer in the session,
# then call admin.site.model_page to render the form
if request.method == 'GET':
request.session[CALLBACK_URL_VAR] = request.META['HTTP_REFERER']
# force it into "popup" mode to hide breadcrumbs, etc
# (since they'll all be aimed at incorrect URLs otherwise)
if '_popup' not in request.GET:
gtmp = request.GET.copy()
gtmp['_popup'] = 1
request.GET = gtmp
return admin.site.model_page(request, app, model, id)
# POST == call model and, if we get a valid HttpResponseRedirect,
# override it with a redirect to the callback URL we saved earlier
elif request.method == 'POST':
response = admin.site.model_page(request, app, model, id)
if isinstance(response, HttpResponseRedirect) and \
CALLBACK_URL_VAR in request.session:
response['Location'] = request.session[CALLBACK_URL_VAR]
del request.session[CALLBACK_URL_VAR]
return response
### my_template.html:
Edit, and return!
Edit, then come back