From cf546e11ac76c8dec527e39ff8ce8249a195ab42 Mon Sep 17 00:00:00 2001 From: Johannes Hoppe Date: Sat, 7 Nov 2015 12:24:38 +0100 Subject: [PATCH] Fixed #21221 -- Made form Media and static template tag use staticfiles if installed. --- django/contrib/admin/helpers.py | 3 +- django/contrib/admin/options.py | 5 +- .../admin/auth/user/change_password.html | 2 +- .../contrib/admin/templates/admin/base.html | 2 +- .../admin/templates/admin/change_form.html | 2 +- .../admin/templates/admin/change_list.html | 2 +- .../templates/admin/change_list_results.html | 2 +- .../templates/admin/delete_confirmation.html | 2 +- .../admin/delete_selected_confirmation.html | 2 +- .../templates/admin/edit_inline/stacked.html | 2 +- .../templates/admin/edit_inline/tabular.html | 2 +- .../contrib/admin/templates/admin/index.html | 2 +- .../contrib/admin/templates/admin/login.html | 2 +- .../admin/templates/admin/popup_response.html | 2 +- .../admin/prepopulated_fields_js.html | 2 +- .../admin/related_widget_wrapper.html | 2 +- .../admin/templates/admin/search_form.html | 2 +- .../registration/password_change_form.html | 2 +- .../contrib/admin/templatetags/admin_list.py | 2 +- .../admin/templatetags/admin_static.py | 12 ++--- django/contrib/admin/widgets.py | 7 ++- .../staticfiles/templatetags/staticfiles.py | 35 ++++--------- django/forms/widgets.py | 13 +++-- django/templatetags/static.py | 11 +++- docs/howto/static-files/index.txt | 12 ++--- docs/intro/overview.txt | 2 +- docs/intro/tutorial06.txt | 6 +-- docs/ref/contrib/admin/javascript.txt | 4 +- docs/ref/contrib/staticfiles.txt | 52 ++++--------------- docs/ref/templates/builtins.txt | 23 ++++---- docs/releases/1.10.txt | 9 +++- docs/releases/1.4.txt | 8 +-- docs/topics/forms/media.txt | 23 ++++++-- tests/staticfiles_tests/test_forms.py | 30 +++++++++++ 34 files changed, 145 insertions(+), 144 deletions(-) create mode 100644 tests/staticfiles_tests/test_forms.py diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 775fb1b5d3..1217bd78df 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -5,7 +5,6 @@ import warnings from django import forms from django.conf import settings -from django.contrib.admin.templatetags.admin_static import static from django.contrib.admin.utils import ( display_for_field, flatten_fieldsets, help_text_for_field, label_for_field, lookup_field, @@ -77,7 +76,7 @@ class Fieldset(object): js = ['vendor/jquery/jquery%s.js' % extra, 'jquery.init.js', 'collapse%s.js' % extra] - return forms.Media(js=[static('admin/js/%s' % url) for url in js]) + return forms.Media(js=['admin/js/%s' % url for url in js]) return forms.Media() media = property(_media) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 24d698f540..4fb5f3ddc8 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -14,7 +14,6 @@ from django.contrib.admin.checks import ( BaseModelAdminChecks, InlineModelAdminChecks, ModelAdminChecks, ) from django.contrib.admin.exceptions import DisallowedModelAdminToField -from django.contrib.admin.templatetags.admin_static import static from django.contrib.admin.templatetags.admin_urls import add_preserved_filters from django.contrib.admin.utils import ( NestedObjects, flatten_fieldsets, get_deleted_objects, @@ -577,7 +576,7 @@ class ModelAdmin(BaseModelAdmin): 'prepopulate%s.js' % extra, 'vendor/xregexp/xregexp.min.js', ] - return forms.Media(js=[static('admin/js/%s' % url) for url in js]) + return forms.Media(js=['admin/js/%s' % url for url in js]) def get_model_perms(self, request): """ @@ -1820,7 +1819,7 @@ class InlineModelAdmin(BaseModelAdmin): 'inlines%s.js' % extra] if self.filter_vertical or self.filter_horizontal: js.extend(['SelectBox.js', 'SelectFilter2.js']) - return forms.Media(js=[static('admin/js/%s' % url) for url in js]) + return forms.Media(js=['admin/js/%s' % url for url in js]) def get_extra(self, request, obj=None, **kwargs): """Hook for customizing the number of extra inline forms.""" diff --git a/django/contrib/admin/templates/admin/auth/user/change_password.html b/django/contrib/admin/templates/admin/auth/user/change_password.html index 124f9279cf..3f13be2b5c 100644 --- a/django/contrib/admin/templates/admin/auth/user/change_password.html +++ b/django/contrib/admin/templates/admin/auth/user/change_password.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n admin_static %} +{% load i18n static %} {% load admin_urls %} {% block extrahead %}{{ block.super }} diff --git a/django/contrib/admin/templates/admin/base.html b/django/contrib/admin/templates/admin/base.html index ad8663f00d..70e137cfe2 100644 --- a/django/contrib/admin/templates/admin/base.html +++ b/django/contrib/admin/templates/admin/base.html @@ -1,4 +1,4 @@ -{% load i18n admin_static %} +{% load i18n static %} {% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %} diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html index 60c68f1d03..82b495c0b6 100644 --- a/django/contrib/admin/templates/admin/change_form.html +++ b/django/contrib/admin/templates/admin/change_form.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n admin_urls admin_static admin_modify %} +{% load i18n admin_urls static admin_modify %} {% block extrahead %}{{ block.super }} diff --git a/django/contrib/admin/templates/admin/change_list.html b/django/contrib/admin/templates/admin/change_list.html index 5d73d74808..1ea38d8117 100644 --- a/django/contrib/admin/templates/admin/change_list.html +++ b/django/contrib/admin/templates/admin/change_list.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n admin_urls admin_static admin_list %} +{% load i18n admin_urls static admin_list %} {% block extrastyle %} {{ block.super }} diff --git a/django/contrib/admin/templates/admin/change_list_results.html b/django/contrib/admin/templates/admin/change_list_results.html index e3d4b2549a..b3d7dd01d3 100644 --- a/django/contrib/admin/templates/admin/change_list_results.html +++ b/django/contrib/admin/templates/admin/change_list_results.html @@ -1,4 +1,4 @@ -{% load i18n admin_static %} +{% load i18n static %} {% if result_hidden_fields %}
{# DIV for HTML validation #} {% for item in result_hidden_fields %}{{ item }}{% endfor %} diff --git a/django/contrib/admin/templates/admin/delete_confirmation.html b/django/contrib/admin/templates/admin/delete_confirmation.html index baaf75b2a0..414b2a0bb5 100644 --- a/django/contrib/admin/templates/admin/delete_confirmation.html +++ b/django/contrib/admin/templates/admin/delete_confirmation.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n admin_urls admin_static %} +{% load i18n admin_urls static %} {% block extrahead %} {{ media }} diff --git a/django/contrib/admin/templates/admin/delete_selected_confirmation.html b/django/contrib/admin/templates/admin/delete_selected_confirmation.html index 27a888f154..00ee2e79e3 100644 --- a/django/contrib/admin/templates/admin/delete_selected_confirmation.html +++ b/django/contrib/admin/templates/admin/delete_selected_confirmation.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n l10n admin_urls admin_static %} +{% load i18n l10n admin_urls static %} {% block extrahead %} {{ media }} diff --git a/django/contrib/admin/templates/admin/edit_inline/stacked.html b/django/contrib/admin/templates/admin/edit_inline/stacked.html index 799ea3b4de..9025a17afb 100644 --- a/django/contrib/admin/templates/admin/edit_inline/stacked.html +++ b/django/contrib/admin/templates/admin/edit_inline/stacked.html @@ -1,4 +1,4 @@ -{% load i18n admin_urls admin_static %} +{% load i18n admin_urls static %}
diff --git a/django/contrib/admin/templates/admin/index.html b/django/contrib/admin/templates/admin/index.html index fba67d78a4..919ea46cfc 100644 --- a/django/contrib/admin/templates/admin/index.html +++ b/django/contrib/admin/templates/admin/index.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n admin_static %} +{% load i18n static %} {% block extrastyle %}{{ block.super }}{% endblock %} diff --git a/django/contrib/admin/templates/admin/login.html b/django/contrib/admin/templates/admin/login.html index f8653e556e..a955c871f7 100644 --- a/django/contrib/admin/templates/admin/login.html +++ b/django/contrib/admin/templates/admin/login.html @@ -1,5 +1,5 @@ {% extends "admin/base_site.html" %} -{% load i18n admin_static %} +{% load i18n static %} {% block extrastyle %}{{ block.super }} {{ form.media }} diff --git a/django/contrib/admin/templates/admin/popup_response.html b/django/contrib/admin/templates/admin/popup_response.html index 6be1159cd4..6e4fac8e8d 100644 --- a/django/contrib/admin/templates/admin/popup_response.html +++ b/django/contrib/admin/templates/admin/popup_response.html @@ -1,4 +1,4 @@ -{% load i18n admin_static %} +{% load i18n static %} {% trans 'Popup closing...' %} diff --git a/django/contrib/admin/templates/admin/prepopulated_fields_js.html b/django/contrib/admin/templates/admin/prepopulated_fields_js.html index 508066a9da..157233993f 100644 --- a/django/contrib/admin/templates/admin/prepopulated_fields_js.html +++ b/django/contrib/admin/templates/admin/prepopulated_fields_js.html @@ -1,4 +1,4 @@ -{% load l10n admin_static %} +{% load l10n static %} +Or if :mod:`~django.contrib.staticfiles` is configured using the +`~django.contib.staticfiles.ManifestStaticFilesStorage`:: + + >>> w = CalendarWidget() + >>> print(w.media) + + + + +.. versionchanged:: 1.10 + + Older versions didn't serve assets using :mod:`django.contrib.staticfiles`. ``Media`` objects ----------------- diff --git a/tests/staticfiles_tests/test_forms.py b/tests/staticfiles_tests/test_forms.py new file mode 100644 index 0000000000..f5bd24186b --- /dev/null +++ b/tests/staticfiles_tests/test_forms.py @@ -0,0 +1,30 @@ +from django.contrib.staticfiles import storage +from django.forms import Media +from django.test import SimpleTestCase, override_settings +from django.utils.six.moves.urllib.parse import urljoin + + +class StaticTestStorage(storage.StaticFilesStorage): + def url(self, name): + return urljoin('https://example.com/assets/', name) + + +@override_settings( + STATIC_URL='http://media.example.com/static/', + INSTALLED_APPS=('django.contrib.staticfiles', ), + STATICFILES_STORAGE='staticfiles_tests.test_forms.StaticTestStorage', +) +class StaticFilesFormsMediaTestCase(SimpleTestCase): + def test_absolute_url(self): + m = Media( + css={'all': ('path/to/css1', '/path/to/css2')}, + js=('/path/to/js1', 'http://media.other.com/path/to/js2', 'https://secure.other.com/path/to/js3'), + ) + self.assertEqual( + str(m), + """ + + + +""" + )