diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 39f22b8d0e..bba5665565 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -306,11 +306,16 @@ class AdminSite(object): """ Returns a dictionary of variables to put in the template context for *every* page in the admin site. + + For sites running on a subpath, use the SCRIPT_NAME value if site_url + hasn't been customized. """ + script_name = request.META['SCRIPT_NAME'] + site_url = script_name if self.site_url == '/' and script_name else self.site_url return { 'site_title': self.site_title, 'site_header': self.site_header, - 'site_url': self.site_url, + 'site_url': site_url, 'has_permission': self.has_permission(request), 'available_apps': self.get_app_list(request), } diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 6916498660..0d1c3fc305 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2513,6 +2513,15 @@ Templates can override or extend base admin templates as described in The URL for the "View site" link at the top of each admin page. By default, ``site_url`` is ``/``. Set it to ``None`` to remove the link. + For sites running on a subpath, the :meth:`each_context` method checks if + the current request has ``request.META['SCRIPT_NAME']`` set and uses that + value if ``site_url`` isn't set to something other than ``/``. + + .. versionchanged:: 1.10 + + The ``SCRIPT_NAME`` support described in the previous paragraph was + added. + .. attribute:: AdminSite.index_title The text to put at the top of the admin index page (a string). By default, diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index ebfae18f24..c955b7764d 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -32,7 +32,9 @@ Minor features :mod:`django.contrib.admin` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -* ... +* For sites running on a subpath, the default :attr:`URL for the "View site" + link ` at the top of each admin page + will now point to ``request.META['SCRIPT_NAME']`` if set, instead of ``/``. :mod:`django.contrib.admindocs` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/admin_views/test_adminsite.py b/tests/admin_views/test_adminsite.py index 575a15c23a..bc2e59923b 100644 --- a/tests/admin_views/test_adminsite.py +++ b/tests/admin_views/test_adminsite.py @@ -51,6 +51,11 @@ class SiteEachContextTest(TestCase): self.assertEqual(ctx['site_url'], '/') self.assertEqual(ctx['has_permission'], True) + def test_each_context_site_url_with_script_name(self): + request = RequestFactory().get(reverse('test_adminsite:index'), SCRIPT_NAME='/my-script-name/') + request.user = self.u1 + self.assertEqual(site.each_context(request)['site_url'], '/my-script-name/') + def test_available_apps(self): ctx = self.ctx apps = ctx['available_apps']