From 22b2fb0ba1d130c0ff0e1658ee9c2224a619a4a7 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 7 Jul 2014 09:54:58 +0200 Subject: [PATCH] Fixed #22939 -- Delayed admin_static backend detection Thanks generalov for the report. --- django/contrib/admin/templatetags/admin_static.py | 15 ++++++++++----- tests/admin_views/tests.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/django/contrib/admin/templatetags/admin_static.py b/django/contrib/admin/templatetags/admin_static.py index 77473c2e008..ce96960a473 100644 --- a/django/contrib/admin/templatetags/admin_static.py +++ b/django/contrib/admin/templatetags/admin_static.py @@ -3,9 +3,14 @@ from django.template import Library register = Library() -if apps.is_installed('django.contrib.staticfiles'): - from django.contrib.staticfiles.templatetags.staticfiles import static -else: - from django.templatetags.static import static +_static = None -static = register.simple_tag(static) +@register.simple_tag +def static(path): + global _static + if _static is None: + if apps.is_installed('django.contrib.staticfiles'): + from django.contrib.staticfiles.templatetags.staticfiles import static as _static + else: + from django.templatetags.static import static as _static + return _static(path) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 6006a0e5044..b2c554da0f6 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -18,6 +18,7 @@ from django.contrib.auth import get_permission_codename from django.contrib.admin import ModelAdmin from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.contrib.admin.models import LogEntry, DELETION +from django.contrib.admin.templatetags.admin_static import static from django.contrib.admin.templatetags.admin_urls import add_preserved_filters from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase from django.contrib.admin.utils import quote @@ -26,6 +27,7 @@ from django.contrib.admin.views.main import IS_POPUP_VAR from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.models import Group, User, Permission from django.contrib.contenttypes.models import ContentType +from django.contrib.staticfiles.storage import staticfiles_storage from django.forms.utils import ErrorList from django.template.response import TemplateResponse from django.test import TestCase, skipUnlessDBFeature @@ -101,6 +103,18 @@ class AdminViewBasicTest(AdminViewBasicTestCase): '/test_admin/%s/admin_views/article/add/' % self.urlbit, status_code=301) + def test_admin_static_template_tag(self): + """ + Test that admin_static.static is pointing to the collectstatic version + (as django.contrib.collectstatic is in installed apps). + """ + old_url = staticfiles_storage.base_url + staticfiles_storage.base_url = '/test/' + try: + self.assertEqual(static('path'), '/test/path') + finally: + staticfiles_storage.base_url = old_url + def test_basic_add_GET(self): """ A smoke test to ensure GET on the add_view works.