Commit 33d8fcde authored by Jannis Leidel's avatar Jannis Leidel
Browse files

Fixed #14693, #14709 -- Backwards incompatible change to rectify the confusion...

Fixed #14693, #14709 -- Backwards incompatible change to rectify the confusion around the STATICFILES_URL and STATICFILES_ROOT settings.

  * Two new global settings that will be used by -- **but are not limited to** -- the staticfiles app: STATIC_ROOT and STATIC_URL.

  * Moving the 'django.contrib.staticfiles.templatetags.staticfiles' template tag to the core ('django.templatetags.static') and renaming it to 'get_static_prefix'.

  * Moving the context processor 'django.contrib.staticfiles.context_processors.staticfiles' to the core ('django.core.context_processors.static') and renaming it to 'static'.

  * Paths in media definitions will use STATIC_URL as the prefix if the value is not None, and falls back to the previously used MEDIA_URL.

Thanks again to the community for constructive criticism and Carl and Russ for sanity-inducing discussions on IRC.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14592 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 9b45f6cd
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -195,9 +195,9 @@ TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
#    'django.core.context_processors.request',
    'django.contrib.messages.context_processors.messages',
    'django.contrib.staticfiles.context_processors.staticfiles',
)

# Output to use in template system for invalid (e.g. misspelled) variables.
@@ -256,13 +256,21 @@ SECRET_KEY = ''
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/"
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT.
# Example: "http://media.lawrence.com"
# Example: "http://media.lawrence.com/media/"
MEDIA_URL = ''

# Absolute path to the directory that holds static files.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = None

# List of upload handler classes to be applied in order.
FILE_UPLOAD_HANDLERS = (
    'django.core.files.uploadhandler.MemoryFileUploadHandler',
@@ -552,14 +560,6 @@ FIXTURE_DIRS = ()
# STATICFILES #
###############

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/static/"
STATICFILES_ROOT = ''

# URL that handles the static files served from STATICFILES_ROOT.
# Example: "http://media.lawrence.com/static/"
STATICFILES_URL = '/static/'

# A list of locations of additional static files
STATICFILES_DIRS = ()

+6 −6
Original line number Diff line number Diff line
@@ -49,16 +49,16 @@ MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory that holds media.
# Absolute path to the directory that holds static files.
# Example: "/home/media/media.lawrence.com/static/"
STATICFILES_ROOT = ''
STATIC_ROOT = ''

# URL that handles the static files served from STATICFILES_ROOT.
# Example: "http://static.lawrence.com/", "http://example.com/static/"
STATICFILES_URL = '/static/'
# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# URL prefix for admin media -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
+3 −7
Original line number Diff line number Diff line
from django.template import Library
from django.utils.encoding import iri_to_uri
from django.templatetags.static import PrefixNode

register = Library()

@register.simple_tag
def admin_media_prefix():
    """
    Returns the string contained in the setting ADMIN_MEDIA_PREFIX.
    """
    try:
        from django.conf import settings
    except ImportError:
        return ''
    return iri_to_uri(settings.ADMIN_MEDIA_PREFIX)
admin_media_prefix = register.simple_tag(admin_media_prefix)
    return PrefixNode.handle_simple("ADMIN_MEDIA_PREFIX")
+0 −6
Original line number Diff line number Diff line
from django.conf import settings

def staticfiles(request):
    return {
        'STATICFILES_URL': settings.STATICFILES_URL,
    }
+17 −19
Original line number Diff line number Diff line
@@ -10,46 +10,44 @@ from django.contrib.staticfiles.views import serve
class StaticFilesHandler(WSGIHandler):
    """
    WSGI middleware that intercepts calls to the static files directory, as
    defined by the STATICFILES_URL setting, and serves those files.
    defined by the STATIC_URL setting, and serves those files.
    """
    def __init__(self, application, media_dir=None):
    def __init__(self, application, base_dir=None):
        self.application = application
        if media_dir:
            self.media_dir = media_dir
        if base_dir:
            self.base_dir = base_dir
        else:
            self.media_dir = self.get_media_dir()
        self.media_url = urlparse(self.get_media_url())
        if settings.DEBUG:
            utils.check_settings()
            self.base_dir = self.get_base_dir()
        self.base_url = urlparse(self.get_base_url())
        super(StaticFilesHandler, self).__init__()

    def get_media_dir(self):
        return settings.STATICFILES_ROOT
    def get_base_dir(self):
        return settings.STATIC_ROOT

    def get_media_url(self):
        return settings.STATICFILES_URL
    def get_base_url(self):
        if settings.DEBUG:
            utils.check_settings()
        return settings.STATIC_URL

    def _should_handle(self, path):
        """
        Checks if the path should be handled. Ignores the path if:

        * the host is provided as part of the media_url
        * the host is provided as part of the base_url
        * the request's path isn't under the media path (or equal)
        * settings.DEBUG isn't True
        """
        return (self.media_url[2] != path and
            path.startswith(self.media_url[2]) and not self.media_url[1])
        return (self.base_url[2] != path and
            path.startswith(self.base_url[2]) and not self.base_url[1])

    def file_path(self, url):
        """
        Returns the relative path to the media file on disk for the given URL.

        The passed URL is assumed to begin with ``media_url``.  If the
        The passed URL is assumed to begin with ``base_url``.  If the
        resultant file path is outside the media directory, then a ValueError
        is raised.
        """
        # Remove ``media_url``.
        relative_url = url[len(self.media_url[2]):]
        relative_url = url[len(self.base_url[2]):]
        return urllib.url2pathname(relative_url)

    def serve(self, request):
Loading