Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Created October 25, 2011 01:17
Show Gist options
  • Select an option

  • Save SmileyChris/1311019 to your computer and use it in GitHub Desktop.

Select an option

Save SmileyChris/1311019 to your computer and use it in GitHub Desktop.
"""
To use these relative storage classes as the default, change the following
settings::
DEFAULT_FILE_STORAGE = 'relative_storage.RelativeFileSystemStorage'
STATICFILES_STORAGE = 'relative_storage.RelativeStaticFilesStorage'
Then, you'll want to make sure that you only use the ``{% static %}`` tag when
referencing static files.
"""
from django.core.files import FileSystemStorage
from django.contrib.auth.staticfiles.storage import StaticFilesStorage
from django.core.urlresolvers import get_script_prefix
class RelativeMixin(object):
"""
A storage mixin class that allows for the base_url to be 'relative' to the
web server's script prefix.
For example, setting MEDIA_URL to 'static/' (note the lack of a leading
slash) will respect the script prefix.
"""
def get_base_url(self):
"""
Return the base url, making it relative to the current script prefix if
the URL is relative.
"""
if self._base_url.startswith('/'):
return self._base_url
return get_script_prefix() + self._base_url
def set_base_url(self, value):
"""
Store the base url value.
"""
self._base_url = value
base_url = property(get_base_url, set_base_url)
class RelativeFileSystemStorage(RelativeMixin, FileSystemStorage):
"""
File system storage which allows for a MEDIA_URL relative to the script
prefix.
"""
class RelativeStaticFilesStorage(RelativeMixin, StaticFilesStorage):
"""
Static files storage which allows for a STATIC_URL relative to the script
prefix.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment