mirror of
https://github.com/django/django.git
synced 2025-10-26 15:16:09 +00:00
Fixed #21221 -- Made form Media and static template tag use staticfiles if installed.
This commit is contained in:
committed by
Tim Graham
parent
6be9589eb3
commit
cf546e11ac
@@ -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)
|
||||
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
{% load admin_urls %}
|
||||
|
||||
{% block extrahead %}{{ block.super }}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_static %}<!DOCTYPE html>
|
||||
{% load i18n static %}<!DOCTYPE html>
|
||||
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
|
||||
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
|
||||
<head>
|
||||
|
||||
@@ -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 }}
|
||||
<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
{% if result_hidden_fields %}
|
||||
<div class="hiddenfields">{# DIV for HTML validation #}
|
||||
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n admin_urls admin_static %}
|
||||
{% load i18n admin_urls static %}
|
||||
|
||||
{% block extrahead %}
|
||||
{{ media }}
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_urls admin_static %}
|
||||
{% load i18n admin_urls static %}
|
||||
<div class="js-inline-admin-formset inline-group"
|
||||
id="{{ inline_admin_formset.formset.prefix }}-group"
|
||||
data-inline-type="stacked"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_urls admin_static admin_modify %}
|
||||
{% load i18n admin_urls static admin_modify %}
|
||||
<div class="js-inline-admin-formset inline-group" id="{{ inline_admin_formset.formset.prefix }}-group"
|
||||
data-inline-type="tabular"
|
||||
data-inline-formset="{{ inline_admin_formset.inline_formset_data }}">
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
|
||||
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}" />{% endblock %}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
|
||||
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/login.css" %}" />
|
||||
{{ form.media }}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_static %}<!DOCTYPE html>
|
||||
{% load i18n static %}<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>{% trans 'Popup closing...' %}</title></head>
|
||||
<body>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load l10n admin_static %}
|
||||
{% load l10n static %}
|
||||
<script type="text/javascript"
|
||||
id="django-admin-prepopulated-fields-constants"
|
||||
src="{% static "admin/js/prepopulate_init.js" %}"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
<div class="related-widget-wrapper">
|
||||
{{ widget }}
|
||||
{% block links %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
{% if cl.search_fields %}
|
||||
<div id="toolbar"><form id="changelist-search" method="get">
|
||||
<div><!-- DIV needed for valid HTML -->
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n admin_static %}
|
||||
{% load i18n static %}
|
||||
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />{% endblock %}
|
||||
{% block userlinks %}{% url 'django-admindocs-docroot' as docsroot %}{% if docsroot %}<a href="{{ docsroot }}">{% trans 'Documentation' %}</a> / {% endif %} {% trans 'Change password' %} / <a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>{% endblock %}
|
||||
{% block breadcrumbs %}
|
||||
|
||||
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||
import datetime
|
||||
import warnings
|
||||
|
||||
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 (
|
||||
display_for_field, display_for_value, label_for_field, lookup_field,
|
||||
@@ -16,6 +15,7 @@ from django.core.urlresolvers import NoReverseMatch
|
||||
from django.db import models
|
||||
from django.template import Library
|
||||
from django.template.loader import get_template
|
||||
from django.templatetags.static import static
|
||||
from django.utils import formats
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
from django.apps import apps
|
||||
from django.template import Library
|
||||
from django.templatetags.static import static as _static
|
||||
|
||||
register = Library()
|
||||
|
||||
_static = None
|
||||
|
||||
|
||||
@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
|
||||
# Backwards compatibility alias for django.templatetags.static.static().
|
||||
# Deprecation should start in Django 2.0.
|
||||
return _static(path)
|
||||
|
||||
@@ -6,7 +6,6 @@ from __future__ import unicode_literals
|
||||
import copy
|
||||
|
||||
from django import forms
|
||||
from django.contrib.admin.templatetags.admin_static import static
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db.models.deletion import CASCADE
|
||||
from django.forms.utils import flatatt
|
||||
@@ -32,7 +31,7 @@ class FilteredSelectMultiple(forms.SelectMultiple):
|
||||
@property
|
||||
def media(self):
|
||||
js = ["core.js", "SelectBox.js", "SelectFilter2.js"]
|
||||
return forms.Media(js=[static("admin/js/%s" % path) for path in js])
|
||||
return forms.Media(js=["admin/js/%s" % path for path in js])
|
||||
|
||||
def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
|
||||
self.verbose_name = verbose_name
|
||||
@@ -56,7 +55,7 @@ class AdminDateWidget(forms.DateInput):
|
||||
@property
|
||||
def media(self):
|
||||
js = ["calendar.js", "admin/DateTimeShortcuts.js"]
|
||||
return forms.Media(js=[static("admin/js/%s" % path) for path in js])
|
||||
return forms.Media(js=["admin/js/%s" % path for path in js])
|
||||
|
||||
def __init__(self, attrs=None, format=None):
|
||||
final_attrs = {'class': 'vDateField', 'size': '10'}
|
||||
@@ -69,7 +68,7 @@ class AdminTimeWidget(forms.TimeInput):
|
||||
@property
|
||||
def media(self):
|
||||
js = ["calendar.js", "admin/DateTimeShortcuts.js"]
|
||||
return forms.Media(js=[static("admin/js/%s" % path) for path in js])
|
||||
return forms.Media(js=["admin/js/%s" % path for path in js])
|
||||
|
||||
def __init__(self, attrs=None, format=None):
|
||||
final_attrs = {'class': 'vTimeField', 'size': '8'}
|
||||
|
||||
@@ -1,36 +1,19 @@
|
||||
from django import template
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
from django.templatetags.static import StaticNode
|
||||
from django.templatetags.static import (
|
||||
do_static as _do_static, static as _static,
|
||||
)
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
def static(path):
|
||||
return staticfiles_storage.url(path)
|
||||
|
||||
|
||||
class StaticFilesNode(StaticNode):
|
||||
|
||||
def url(self, context):
|
||||
path = self.path.resolve(context)
|
||||
return static(path)
|
||||
# Backwards compatibility alias for django.templatetags.static.static().
|
||||
# Deprecation should start in Django 2.0.
|
||||
return _static(path)
|
||||
|
||||
|
||||
@register.tag('static')
|
||||
def do_static(parser, token):
|
||||
"""
|
||||
A template tag that returns the URL to a file
|
||||
using staticfiles' storage backend
|
||||
|
||||
Usage::
|
||||
|
||||
{% static path [as varname] %}
|
||||
|
||||
Examples::
|
||||
|
||||
{% static "myapp/css/base.css" %}
|
||||
{% static variable_with_path %}
|
||||
{% static "myapp/css/base.css" as admin_base_css %}
|
||||
{% static variable_with_path as varname %}
|
||||
"""
|
||||
return StaticFilesNode.handle_token(parser, token)
|
||||
# Backwards compatibility alias for django.templatetags.static.do_static().
|
||||
# Deprecation should start in Django 2.0.
|
||||
return _do_static(parser, token)
|
||||
|
||||
@@ -11,6 +11,7 @@ from itertools import chain
|
||||
|
||||
from django.conf import settings
|
||||
from django.forms.utils import flatatt, to_current_timezone
|
||||
from django.templatetags.static import static
|
||||
from django.utils import datetime_safe, formats, six
|
||||
from django.utils.datastructures import MultiValueDict
|
||||
from django.utils.dates import MONTHS
|
||||
@@ -21,7 +22,6 @@ from django.utils.formats import get_format
|
||||
from django.utils.html import conditional_escape, format_html, html_safe
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.six.moves import range
|
||||
from django.utils.six.moves.urllib.parse import urljoin
|
||||
from django.utils.translation import ugettext_lazy
|
||||
|
||||
__all__ = (
|
||||
@@ -77,12 +77,15 @@ class Media(object):
|
||||
) for path in self._css[medium]
|
||||
] for medium in media])
|
||||
|
||||
def absolute_path(self, path, prefix=None):
|
||||
def absolute_path(self, path):
|
||||
"""
|
||||
Given a relative or absolute path to a static asset, return an absolute
|
||||
path. An absolute path will be returned unchanged while a relative path
|
||||
will be passed to django.templatetags.static.static().
|
||||
"""
|
||||
if path.startswith(('http://', 'https://', '/')):
|
||||
return path
|
||||
if prefix is None:
|
||||
prefix = settings.STATIC_URL
|
||||
return urljoin(prefix, path)
|
||||
return static(path)
|
||||
|
||||
def __getitem__(self, name):
|
||||
"Returns a Media object that only contains media of the given type"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from django import template
|
||||
from django.apps import apps
|
||||
from django.utils.encoding import iri_to_uri
|
||||
from django.utils.six.moves.urllib.parse import urljoin
|
||||
|
||||
@@ -108,7 +109,11 @@ class StaticNode(template.Node):
|
||||
|
||||
@classmethod
|
||||
def handle_simple(cls, path):
|
||||
return urljoin(PrefixNode.handle_simple("STATIC_URL"), path)
|
||||
if apps.is_installed('django.contrib.staticfiles'):
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
return staticfiles_storage.url(path)
|
||||
else:
|
||||
return urljoin(PrefixNode.handle_simple("STATIC_URL"), path)
|
||||
|
||||
@classmethod
|
||||
def handle_token(cls, parser, token):
|
||||
@@ -151,4 +156,8 @@ def do_static(parser, token):
|
||||
|
||||
|
||||
def static(path):
|
||||
"""
|
||||
Given a relative path to a static asset, return the absolute path to the
|
||||
asset.
|
||||
"""
|
||||
return StaticNode.handle_simple(path)
|
||||
|
||||
Reference in New Issue
Block a user