I have a client that has an app built with django. On every page of
their app is a link to their admin site. They tell me the admin site
is generated entirely by django, and they’ve never customized it
before. On the very first line of the admin page it says:
Django administration Welcome, admin. Change password / Log out
They want me to add a link to that line, to the left of “Django
administration” that will take them back to the page they were on when
they clicked on the link to get them to the admin site.
So I have 2 issues here:
-
How do I override that line to add the link? It appears that page
is generated by contrib/admin/templates/admin/base.html, and I tried
to override it by following the directions at
https://docs.djangoproject.com/en/1.2/ref/contrib/admin/#overriding-a…,
but whatever I do seems to have no effect. -
How can I get the link of the page of the app they came from? It’s
not simply just going back one page, as they could have navigated all
over the place of the admin site before clicking the “Back to app”
link.
There are many ways to store the last visited non-admin url in request.session. For example, a middleware:
Then override the template:
Store the last non admin url in request.session, e.g. put the above class in
yourproject/middleware.py, add tosettings.MIDDLEWARE_CLASSES:middleware.LastSiteUrlPrepare the admin base site template for overriding, copy
django/contrib/admin/templates/admin/base_site.htmltoyourproject/templates/admin/base_site.htmlLink to request.session.last_site_url, e.g. in
yourproject/templates/admin/base_site.html, find{% block branding %}, before the H1 tag of this block, add an HTML link to{{ request.session.last_site_url }}.It should look like that: