-
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
index fab2d0f..8ebaba6 100644
|
a
|
b
|
MEDIA_ROOT = ''
|
| 263 | 263 | # Example: "http://media.lawrence.com" |
| 264 | 264 | MEDIA_URL = '' |
| 265 | 265 | |
| | 266 | # Absolute path to the directory that holds static files. |
| | 267 | # Example: "/home/media/media.lawrence.com/static/" |
| | 268 | STATIC_ROOT = '' |
| | 269 | |
| | 270 | # URL that handles the static files served from STATIC_ROOT. |
| | 271 | # Example: "http://media.lawrence.com/static/" |
| | 272 | STATIC_URL = None |
| | 273 | |
| 266 | 274 | # List of upload handler classes to be applied in order. |
| 267 | 275 | FILE_UPLOAD_HANDLERS = ( |
| 268 | 276 | 'django.core.files.uploadhandler.MemoryFileUploadHandler', |
| … |
… |
FIXTURE_DIRS = ()
|
| 552 | 560 | # STATICFILES # |
| 553 | 561 | ############### |
| 554 | 562 | |
| 555 | | # Absolute path to the directory that holds media. |
| 556 | | # Example: "/home/media/media.lawrence.com/static/" |
| 557 | | STATICFILES_ROOT = '' |
| 558 | | |
| 559 | | # URL that handles the static files served from STATICFILES_ROOT. |
| 560 | | # Example: "http://media.lawrence.com/static/" |
| 561 | | STATICFILES_URL = '/static/' |
| 562 | | |
| 563 | 563 | # A list of locations of additional static files |
| 564 | 564 | STATICFILES_DIRS = () |
| 565 | 565 | |
-
diff --git a/django/conf/project_template/settings.py b/django/conf/project_template/settings.py
index 07a823b..19d5ccd 100644
|
a
|
b
|
MEDIA_URL = ''
|
| 54 | 54 | |
| 55 | 55 | # Absolute path to the directory that holds media. |
| 56 | 56 | # Example: "/home/media/media.lawrence.com/static/" |
| 57 | | STATICFILES_ROOT = '' |
| | 57 | STATIC_ROOT = '' |
| 58 | 58 | |
| 59 | | # URL that handles the static files served from STATICFILES_ROOT. |
| | 59 | # URL that handles the static files served from STATIC_ROOT. |
| 60 | 60 | # Example: "http://static.lawrence.com/", "http://example.com/static/" |
| 61 | | STATICFILES_URL = '/static/' |
| | 61 | STATIC_URL = '/static/' |
| 62 | 62 | |
| 63 | 63 | # URL prefix for admin media -- CSS, JavaScript and images. |
| 64 | 64 | # Make sure to use a trailing slash. |
-
diff --git a/django/contrib/staticfiles/context_processors.py b/django/contrib/staticfiles/context_processors.py
index 3ed0071..dafd3c2 100644
|
a
|
b
|
from django.conf import settings
|
| 2 | 2 | |
| 3 | 3 | def staticfiles(request): |
| 4 | 4 | return { |
| 5 | | 'STATICFILES_URL': settings.STATICFILES_URL, |
| | 5 | 'STATIC_URL': settings.STATIC_URL, |
| 6 | 6 | } |
-
diff --git a/django/contrib/staticfiles/handlers.py b/django/contrib/staticfiles/handlers.py
index 20b0496..a7ac1a2 100644
|
a
|
b
|
from django.contrib.staticfiles.views import serve
|
| 10 | 10 | class StaticFilesHandler(WSGIHandler): |
| 11 | 11 | """ |
| 12 | 12 | WSGI middleware that intercepts calls to the static files directory, as |
| 13 | | defined by the STATICFILES_URL setting, and serves those files. |
| | 13 | defined by the STATIC_URL setting, and serves those files. |
| 14 | 14 | """ |
| 15 | 15 | def __init__(self, application, media_dir=None): |
| 16 | 16 | self.application = application |
| … |
… |
class StaticFilesHandler(WSGIHandler):
|
| 24 | 24 | super(StaticFilesHandler, self).__init__() |
| 25 | 25 | |
| 26 | 26 | def get_media_dir(self): |
| 27 | | return settings.STATICFILES_ROOT |
| | 27 | return settings.STATIC_ROOT |
| 28 | 28 | |
| 29 | 29 | def get_media_url(self): |
| 30 | | return settings.STATICFILES_URL |
| | 30 | return settings.STATIC_URL |
| 31 | 31 | |
| 32 | 32 | def _should_handle(self, path): |
| 33 | 33 | """ |
-
diff --git a/django/contrib/staticfiles/management/commands/collectstatic.py b/django/contrib/staticfiles/management/commands/collectstatic.py
index d121223..8882ae2 100644
|
a
|
b
|
from django.contrib.staticfiles import finders
|
| 12 | 12 | class Command(NoArgsCommand): |
| 13 | 13 | """ |
| 14 | 14 | Command that allows to copy or symlink media files from different |
| 15 | | locations to the settings.STATICFILES_ROOT. |
| | 15 | locations to the settings.STATIC_ROOT. |
| 16 | 16 | """ |
| 17 | 17 | option_list = NoArgsCommand.option_list + ( |
| 18 | 18 | make_option('--noinput', action='store_false', dest='interactive', |
| … |
… |
Type 'yes' to continue, or 'no' to cancel: """)
|
| 85 | 85 | self.stdout.write("\n%s static file%s %s to '%s'%s.\n" |
| 86 | 86 | % (actual_count, actual_count != 1 and 's' or '', |
| 87 | 87 | symlink and 'symlinked' or 'copied', |
| 88 | | settings.STATICFILES_ROOT, |
| | 88 | settings.STATIC_ROOT, |
| 89 | 89 | unmodified_count and ' (%s unmodified)' |
| 90 | 90 | % unmodified_count or '')) |
| 91 | 91 | |
-
diff --git a/django/contrib/staticfiles/management/commands/runserver.py b/django/contrib/staticfiles/management/commands/runserver.py
index e138759..f4c2259 100644
|
a
|
b
|
from django.contrib.staticfiles.handlers import StaticFilesHandler
|
| 8 | 8 | class Command(BaseRunserverCommand): |
| 9 | 9 | option_list = BaseRunserverCommand.option_list + ( |
| 10 | 10 | make_option('--nostatic', action="store_false", dest='use_static_handler', default=True, |
| 11 | | help='Tells Django to NOT automatically serve static files at STATICFILES_URL.'), |
| | 11 | help='Tells Django to NOT automatically serve static files at STATIC_URL.'), |
| 12 | 12 | make_option('--insecure', action="store_true", dest='insecure_serving', default=False, |
| 13 | 13 | help='Allows serving static files even if DEBUG is False.'), |
| 14 | 14 | ) |
-
diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
index b4bfea7..db55895 100644
|
a
|
b
|
class StaticFilesStorage(FileSystemStorage):
|
| 12 | 12 | Standard file system storage for site media files. |
| 13 | 13 | |
| 14 | 14 | The defaults for ``location`` and ``base_url`` are |
| 15 | | ``STATICFILES_ROOT`` and ``STATICFILES_URL``. |
| | 15 | ``STATIC_ROOT`` and ``STATIC_URL``. |
| 16 | 16 | """ |
| 17 | 17 | def __init__(self, location=None, base_url=None, *args, **kwargs): |
| 18 | 18 | if location is None: |
| 19 | | location = settings.STATICFILES_ROOT |
| | 19 | location = settings.STATIC_ROOT |
| 20 | 20 | if base_url is None: |
| 21 | | base_url = settings.STATICFILES_URL |
| | 21 | base_url = settings.STATIC_URL |
| 22 | 22 | if not location: |
| 23 | 23 | raise ImproperlyConfigured("You're using the staticfiles app " |
| 24 | | "without having set the STATICFILES_ROOT setting. Set it to " |
| | 24 | "without having set the STATIC_ROOT setting. Set it to " |
| 25 | 25 | "the absolute path of the directory that holds static media.") |
| 26 | 26 | if not base_url: |
| 27 | 27 | raise ImproperlyConfigured("You're using the staticfiles app " |
| 28 | | "without having set the STATICFILES_URL setting. Set it to " |
| 29 | | "URL that handles the files served from STATICFILES_ROOT.") |
| | 28 | "without having set the STATIC_URL setting. Set it to " |
| | 29 | "URL that handles the files served from STATIC_ROOT.") |
| 30 | 30 | if settings.DEBUG: |
| 31 | 31 | utils.check_settings() |
| 32 | 32 | super(StaticFilesStorage, self).__init__(location, base_url, *args, **kwargs) |
-
diff --git a/django/contrib/staticfiles/templatetags/staticfiles.py b/django/contrib/staticfiles/templatetags/staticfiles.py
deleted file mode 100644
index 6153f5a..0000000
|
+
|
-
|
|
| 1 | | from django import template |
| 2 | | from django.utils.encoding import iri_to_uri |
| 3 | | |
| 4 | | register = template.Library() |
| 5 | | |
| 6 | | class StaticFilesPrefixNode(template.Node): |
| 7 | | |
| 8 | | def __init__(self, varname=None): |
| 9 | | self.varname = varname |
| 10 | | |
| 11 | | def render(self, context): |
| 12 | | try: |
| 13 | | from django.conf import settings |
| 14 | | except ImportError: |
| 15 | | prefix = '' |
| 16 | | else: |
| 17 | | prefix = iri_to_uri(settings.STATICFILES_URL) |
| 18 | | if self.varname is None: |
| 19 | | return prefix |
| 20 | | context[self.varname] = prefix |
| 21 | | return '' |
| 22 | | |
| 23 | | @register.tag |
| 24 | | def get_staticfiles_prefix(parser, token): |
| 25 | | """ |
| 26 | | Populates a template variable with the prefix (settings.STATICFILES_URL). |
| 27 | | |
| 28 | | Usage:: |
| 29 | | |
| 30 | | {% get_staticfiles_prefix [as varname] %} |
| 31 | | |
| 32 | | Examples:: |
| 33 | | |
| 34 | | {% get_staticfiles_prefix %} |
| 35 | | {% get_staticfiles_prefix as staticfiles_prefix %} |
| 36 | | |
| 37 | | """ |
| 38 | | tokens = token.contents.split() |
| 39 | | if len(tokens) > 1 and tokens[1] != 'as': |
| 40 | | raise template.TemplateSyntaxError( |
| 41 | | "First argument in '%s' must be 'as'" % tokens[0]) |
| 42 | | return StaticFilesPrefixNode(varname=(len(tokens) > 1 and tokens[2] or None)) |
| 43 | | |
-
diff --git a/django/contrib/staticfiles/urls.py b/django/contrib/staticfiles/urls.py
index 70f04f2..952491b 100644
|
a
|
b
|
if settings.DEBUG:
|
| 11 | 11 | url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'), |
| 12 | 12 | ) |
| 13 | 13 | |
| 14 | | def staticfiles_urlpatterns(prefix=None): |
| | 14 | def static_urlpatterns(prefix=None): |
| 15 | 15 | """ |
| 16 | 16 | Helper function to return a URL pattern for serving static files. |
| 17 | 17 | """ |
| 18 | 18 | if not settings.DEBUG: |
| 19 | 19 | return [] |
| 20 | 20 | if prefix is None: |
| 21 | | prefix = settings.STATICFILES_URL |
| | 21 | prefix = settings.STATIC_URL |
| 22 | 22 | if not prefix: |
| 23 | 23 | raise ImproperlyConfigured( |
| 24 | | "The prefix for the 'staticfiles_urlpatterns' helper is empty. " |
| 25 | | "Make sure the STATICFILES_URL setting is set correctly.") |
| | 24 | "The prefix for the 'static_urlpatterns' helper is empty. " |
| | 25 | "Make sure the STATIC_URL setting is set correctly.") |
| 26 | 26 | if '://' in prefix: |
| 27 | 27 | raise ImproperlyConfigured( |
| 28 | | "The STATICFILES_URL setting is a full URL, not a path and " |
| 29 | | "can't be used with the 'staticfiles_urlpatterns' helper.") |
| | 28 | "The STATIC_URL setting is a full URL, not a path and " |
| | 29 | "can't be used with the 'static_urlpatterns' helper.") |
| 30 | 30 | if prefix.startswith("/"): |
| 31 | 31 | prefix = prefix[1:] |
| 32 | 32 | return patterns('', |
-
diff --git a/django/contrib/staticfiles/utils.py b/django/contrib/staticfiles/utils.py
index 0071dbd..4efcb47 100644
|
a
|
b
|
def check_settings():
|
| 36 | 36 | Checks if the MEDIA_(ROOT|URL) and STATICFILES_(ROOT|URL) |
| 37 | 37 | settings have the same value. |
| 38 | 38 | """ |
| 39 | | if settings.MEDIA_URL == settings.STATICFILES_URL: |
| 40 | | raise ImproperlyConfigured("The MEDIA_URL and STATICFILES_URL " |
| | 39 | if settings.MEDIA_URL == settings.STATIC_URL: |
| | 40 | raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL " |
| 41 | 41 | "settings must have individual values") |
| 42 | | if ((settings.MEDIA_ROOT and settings.STATICFILES_ROOT) and |
| 43 | | (settings.MEDIA_ROOT == settings.STATICFILES_ROOT)): |
| 44 | | raise ImproperlyConfigured("The MEDIA_ROOT and STATICFILES_ROOT " |
| | 42 | if ((settings.MEDIA_ROOT and settings.STATIC_ROOT) and |
| | 43 | (settings.MEDIA_ROOT == settings.STATIC_ROOT)): |
| | 44 | raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT " |
| 45 | 45 | "settings must have individual values") |
-
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index cb12586..2b98638 100644
|
a
|
b
|
|
| 1 | 1 | """ |
| 2 | 2 | HTML Widget classes |
| 3 | 3 | """ |
| | 4 | import datetime |
| | 5 | from itertools import chain |
| | 6 | import time |
| | 7 | from urlparse import urljoin |
| | 8 | from util import flatatt |
| 4 | 9 | |
| 5 | 10 | import django.utils.copycompat as copy |
| 6 | | from itertools import chain |
| 7 | 11 | from django.conf import settings |
| 8 | 12 | from django.utils.datastructures import MultiValueDict, MergeDict |
| 9 | 13 | from django.utils.html import escape, conditional_escape |
| … |
… |
from django.utils.translation import ugettext, ugettext_lazy
|
| 11 | 15 | from django.utils.encoding import StrAndUnicode, force_unicode |
| 12 | 16 | from django.utils.safestring import mark_safe |
| 13 | 17 | from django.utils import datetime_safe, formats |
| 14 | | import time |
| 15 | | import datetime |
| 16 | | from util import flatatt |
| 17 | | from urlparse import urljoin |
| 18 | 18 | |
| 19 | 19 | __all__ = ( |
| 20 | 20 | 'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput', |
| … |
… |
class Media(StrAndUnicode):
|
| 63 | 63 | for path in self._css[medium]] |
| 64 | 64 | for medium in media]) |
| 65 | 65 | |
| 66 | | def absolute_path(self, path): |
| | 66 | def absolute_path(self, path, prefix=None): |
| 67 | 67 | if path.startswith(u'http://') or path.startswith(u'https://') or path.startswith(u'/'): |
| 68 | 68 | return path |
| 69 | | return urljoin(settings.MEDIA_URL,path) |
| | 69 | if prefix is None and settings.STATIC_URL is not None: |
| | 70 | prefix = settings.STATIC_URL |
| | 71 | else: |
| | 72 | prefix = settings.MEDIA_URL # backwards compatibility |
| | 73 | return urljoin(prefix, path) |
| 70 | 74 | |
| 71 | 75 | def __getitem__(self, name): |
| 72 | 76 | "Returns a Media object that only contains media of the given type" |
-
diff --git a/docs/howto/static-files.txt b/docs/howto/static-files.txt
index 37f1fc3..51ec2d2 100644
|
a
|
b
|
Here's the basic usage in a nutshell:
|
| 50 | 50 | First, you'll need to make sure that ``django.contrib.staticfiles`` is in |
| 51 | 51 | your :setting:`INSTALLED_APPS`. |
| 52 | 52 | |
| 53 | | Next, you'll need to edit :setting:`STATICFILES_ROOT` to point to where |
| | 53 | Next, you'll need to edit :setting:`STATIC_ROOT` to point to where |
| 54 | 54 | you'd like your static media stored. For example:: |
| 55 | 55 | |
| 56 | | STATICFILES_ROOT = "/home/jacob/projects/mysite.com/static_media" |
| | 56 | STATIC_ROOT = "/home/jacob/projects/mysite.com/static_media" |
| 57 | 57 | |
| 58 | | You may also want to set the :setting:`STATICFILES_URL` setting at this |
| | 58 | You may also want to set the :setting:`STATIC_URL` setting at this |
| 59 | 59 | time, though the default value (of ``/static/``) is perfect for local |
| 60 | 60 | development. |
| 61 | 61 | |
| … |
… |
Here's the basic usage in a nutshell:
|
| 69 | 69 | ./manage.py collectstatic |
| 70 | 70 | |
| 71 | 71 | This'll churn through your static file storage and move them into the |
| 72 | | directory given by :setting:`STATICFILES_ROOT`. (This is not necessary |
| | 72 | directory given by :setting:`STATIC_ROOT`. (This is not necessary |
| 73 | 73 | in local development if you are using :djadmin:`runserver` or adding |
| 74 | | ``staticfiles_urlpatterns`` to your URLconf; see below). |
| | 74 | ``static_urlpatterns`` to your URLconf; see below). |
| 75 | 75 | |
| 76 | 76 | 4. Deploy that media. |
| 77 | 77 | |
| 78 | 78 | If you're using the built-in development server (the |
| 79 | 79 | :djadmin:`runserver` management command) and have the :setting:`DEBUG` |
| 80 | 80 | setting set to ``True``, your staticfiles will automatically be served |
| 81 | | from :setting:`STATICFILES_URL` in development. |
| | 81 | from :setting:`STATIC_URL` in development. |
| 82 | 82 | |
| 83 | 83 | If you are using some other server for local development, you can |
| 84 | 84 | quickly serve static media locally by adding:: |
| 85 | 85 | |
| 86 | | from django.contrib.staticfiles.urls import staticfiles_urlpatterns |
| 87 | | urlpatterns += staticfiles_urlpatterns() |
| | 86 | from django.contrib.staticfiles.urls import static_urlpatterns |
| | 87 | urlpatterns += static_urlpatterns() |
| 88 | 88 | |
| 89 | 89 | to the bottom of your URLconf. See :ref:`staticfiles-development` for |
| 90 | 90 | details. |
| … |
… |
Here's the basic usage in a nutshell:
|
| 98 | 98 | |
| 99 | 99 | .. code-block:: html+django |
| 100 | 100 | |
| 101 | | <img src="{{ STATICFILES_URL }}images/hi.jpg /> |
| | 101 | <img src="{{ STATIC_URL }}images/hi.jpg /> |
| 102 | 102 | |
| 103 | 103 | See :ref:`staticfiles-in-templates` for more details, including an |
| 104 | 104 | alternate method (using a template tag). |
| … |
… |
the framework see :doc:`the staticfiles reference </ref/contrib/staticfiles>`.
|
| 115 | 115 | app is to make it easier to keep static files separate from user-uploaded |
| 116 | 116 | files. For this reason, you will probably want to make your |
| 117 | 117 | :setting:`MEDIA_ROOT` and :setting:`MEDIA_URL` different from your |
| 118 | | :setting:`STATICFILES_ROOT` and :setting:`STATICFILES_URL`. You will need to |
| | 118 | :setting:`STATIC_ROOT` and :setting:`STATIC_URL`. You will need to |
| 119 | 119 | arrange for serving of files in :setting:`MEDIA_ROOT` yourself; |
| 120 | 120 | ``staticfiles`` does not deal with user-uploaded media at all. |
| 121 | 121 | |
| … |
… |
development, and it makes it *very* hard to change where you've deployed your
|
| 136 | 136 | media. If, for example, you wanted to switch to using a content delivery network |
| 137 | 137 | (CDN), then you'd need to change more or less every single template. |
| 138 | 138 | |
| 139 | | A far better way is to use the value of the :setting:`STATICFILES_URL` setting |
| | 139 | A far better way is to use the value of the :setting:`STATIC_URL` setting |
| 140 | 140 | directly in your templates. This means that a switch of media servers only |
| 141 | 141 | requires changing that single value. Much better! |
| 142 | 142 | |
| … |
… |
editing that setting by hand it should look something like::
|
| 160 | 160 | 'django.contrib.staticfiles.context_processors.staticfiles', |
| 161 | 161 | ) |
| 162 | 162 | |
| 163 | | Once that's done, you can refer to :setting:`STATICFILES_URL` in your templates: |
| | 163 | Once that's done, you can refer to :setting:`STATIC_URL` in your templates: |
| 164 | 164 | |
| 165 | 165 | .. code-block:: html+django |
| 166 | 166 | |
| 167 | | <img src="{{ STATICFILES_URL }}images/hi.jpg /> |
| | 167 | <img src="{{ STATIC_URL }}images/hi.jpg /> |
| 168 | 168 | |
| 169 | | If ``{{ STATICFILES_URL }}`` isn't working in your template, you're probably not |
| | 169 | If ``{{ STATIC_URL }}`` isn't working in your template, you're probably not |
| 170 | 170 | using :class:`~django.template.RequestContext` when rendering the template. |
| 171 | 171 | |
| 172 | 172 | As a brief refresher, context processors add variables into the contexts of |
| … |
… |
To see how that works, and to read more details, check out
|
| 180 | 180 | With a template tag |
| 181 | 181 | ------------------- |
| 182 | 182 | |
| 183 | | The second option is the :ttag:`get_staticfiles_prefix` template tag. You can |
| | 183 | The second option is the :ttag:`get_static_prefix` template tag. You can |
| 184 | 184 | use this if you're not using :class:`~django.template.RequestContext`, or if you |
| 185 | | need more control over exactly where and how :setting:`STATICFILES_URL` is |
| | 185 | need more control over exactly where and how :setting:`STATIC_URL` is |
| 186 | 186 | injected into the template. Here's an example: |
| 187 | 187 | |
| 188 | 188 | .. code-block:: html+django |
| 189 | 189 | |
| 190 | 190 | {% load staticfiles %} |
| 191 | | <img src="{% get_staticfiles_prefix %}images/hi.jpg" /> |
| | 191 | <img src="{% get_static_prefix %}images/hi.jpg" /> |
| 192 | 192 | |
| 193 | 193 | There's also a second form you can use to avoid extra processing if you need the |
| 194 | 194 | value multiple times: |
| … |
… |
value multiple times:
|
| 196 | 196 | .. code-block:: html+django |
| 197 | 197 | |
| 198 | 198 | {% load staticfiles %} |
| 199 | | {% get_staticfiles_prefix as STATIC_PREFIX %} |
| | 199 | {% get_static_prefix as STATIC_PREFIX %} |
| 200 | 200 | |
| 201 | 201 | <img src="{{ STATIC_PREFIX }}images/hi.jpg" /> |
| 202 | 202 | <img src="{{ STATIC_PREFIX }}images/hi2.jpg" /> |
| … |
… |
Thus, the ``staticfiles`` app ships with a quick and dirty helper view that you
|
| 213 | 213 | can use to serve files locally in development. |
| 214 | 214 | |
| 215 | 215 | This view is automatically enabled and will serve your static files at |
| 216 | | :setting:`STATICFILES_URL` when you use the built-in :djadmin:`runserver`. |
| | 216 | :setting:`STATIC_URL` when you use the built-in :djadmin:`runserver`. |
| 217 | 217 | |
| 218 | 218 | To enable this view if you are using some other server for local development, |
| 219 | 219 | you'll add a couple of lines to your URLconf. The first line goes at the top of |
| 220 | 220 | the file, and the last line at the bottom:: |
| 221 | 221 | |
| 222 | | from django.contrib.staticfiles.urls import staticfiles_urlpatterns |
| | 222 | from django.contrib.staticfiles.urls import static_urlpatterns |
| 223 | 223 | |
| 224 | 224 | # ... the rest of your URLconf goes here ... |
| 225 | 225 | |
| 226 | | urlpatterns += staticfiles_urlpatterns() |
| | 226 | urlpatterns += static_urlpatterns() |
| 227 | 227 | |
| 228 | | This will inspect your :setting:`STATICFILES_URL` and |
| 229 | | :setting:`STATICFILES_ROOT` settings and wire up the view to serve static media |
| | 228 | This will inspect your :setting:`STATIC_URL` and |
| | 229 | :setting:`STATIC_ROOT` settings and wire up the view to serve static media |
| 230 | 230 | accordingly. Don't forget to set the :setting:`STATICFILES_DIRS` setting |
| 231 | 231 | appropriately to let ``django.contrib.staticfiles`` know where to look for |
| 232 | 232 | files. |
| … |
… |
Serving static files in production
|
| 249 | 249 | |
| 250 | 250 | The basic outline of putting static files into production is simple: run the |
| 251 | 251 | :djadmin:`collectstatic` command when static media changes, then arrange for the |
| 252 | | collected media directory (:setting:`STATICFILES_ROOT`) to be moved to the media |
| | 252 | collected media directory (:setting:`STATIC_ROOT`) to be moved to the media |
| 253 | 253 | server and served. |
| 254 | 254 | |
| 255 | 255 | Of course, as with all deployment tasks, the devil's in the details. Every |
| … |
… |
app, the basic outline gets modified to look something like:
|
| 264 | 264 | |
| 265 | 265 | * Push your code up to the deployment server. |
| 266 | 266 | * On the server, run :djadmin:`collectstatic` to move all the media into |
| 267 | | :setting:`STATICFILES_ROOT`. |
| 268 | | * Point your web server at :setting:`STATICFILES_ROOT`. For example, here's |
| | 267 | :setting:`STATIC_ROOT`. |
| | 268 | * Point your web server at :setting:`STATIC_ROOT`. For example, here's |
| 269 | 269 | :ref:`how to do this under Apache and mod_wsgi <serving-media-files>`. |
| 270 | 270 | |
| 271 | 271 | You'll probably want to automate this process, especially if you've got multiple |
| … |
… |
Since your media server won't be running Django, you'll need to modify the
|
| 322 | 322 | deployment strategy to look something like: |
| 323 | 323 | |
| 324 | 324 | * When your media changes, run :djadmin:`collectstatic` locally. |
| 325 | | * Push your local :setting:`STATICFILES_ROOT` up to the media server |
| | 325 | * Push your local :setting:`STATIC_ROOT` up to the media server |
| 326 | 326 | into the directory that's being served. ``rsync`` is a good |
| 327 | 327 | choice for this step since it only needs to transfer the |
| 328 | 328 | bits of static media that have changed. |
| … |
… |
you'll need to make a few changes:
|
| 403 | 403 | * The management commands ``build_static`` and ``resolve_static`` are now |
| 404 | 404 | called :djadmin:`collectstatic` and :djadmin:`findstatic`. |
| 405 | 405 | |
| 406 | | * The settings ``STATIC_URL`` and ``STATIC_ROOT`` were renamed to |
| 407 | | :setting:`STATICFILES_URL` and :setting:`STATICFILES_ROOT`. |
| 408 | | |
| 409 | 406 | * The settings ``STATICFILES_PREPEND_LABEL_APPS``, |
| 410 | 407 | ``STATICFILES_MEDIA_DIRNAMES`` and ``STATICFILES_EXCLUDED_APPS`` were |
| 411 | 408 | removed. |
-
diff --git a/docs/man/django-admin.1 b/docs/man/django-admin.1
index 2197af6..f9b530a 100644
|
a
|
b
|
Do not prompt the user for input.
|
| 165 | 165 | Disable the development server's auto\-reloader. |
| 166 | 166 | .TP |
| 167 | 167 | .I \-\-nostatic |
| 168 | | Disable automatic serving of static files from STATICFILES_URL. |
| | 168 | Disable automatic serving of static files from STATIC_URL. |
| 169 | 169 | .TP |
| 170 | 170 | .I \-\-insecure |
| 171 | 171 | Enables serving of static files even if DEBUG is False. |
-
diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt
index f82fd79..4fe38d2 100644
|
a
|
b
|
Settings
|
| 24 | 24 | .. highlight:: python |
| 25 | 25 | |
| 26 | 26 | The following settings control the behavior of the staticfiles app. Only |
| 27 | | :setting:`STATICFILES_ROOT` is required, but you'll probably also need to |
| 28 | | configure :setting:`STATICFILES_URL` as well. |
| | 27 | :setting:`STATIC_ROOT` is required, but you'll probably also need to |
| | 28 | configure :setting:`STATIC_URL` as well. |
| 29 | 29 | |
| 30 | | .. setting:: STATICFILES_ROOT |
| | 30 | .. setting:: STATIC_ROOT |
| 31 | 31 | |
| 32 | | STATICFILES_ROOT |
| 33 | | ---------------- |
| | 32 | STATIC_ROOT |
| | 33 | ----------- |
| 34 | 34 | |
| 35 | 35 | Default: ``''`` (Empty string) |
| 36 | 36 | |
| 37 | 37 | The absolute path to the directory that the :djadmin:`collectstatic` management |
| 38 | 38 | command will collect static files into, for serving from |
| 39 | | :setting:`STATICFILES_URL`:: |
| | 39 | :setting:`STATIC_URL`:: |
| 40 | 40 | |
| 41 | | STATICFILES_ROOT = "/home/example.com/static/" |
| | 41 | STATIC_ROOT = "/home/example.com/static/" |
| 42 | 42 | |
| 43 | 43 | This is a **required setting** unless you've overridden |
| 44 | 44 | :setting:`STATICFILES_STORAGE` and are using a custom storage backend. |
| … |
… |
This is not a place to store your static files permanently under version
|
| 47 | 47 | control; you should do that in directories that will be found by your |
| 48 | 48 | :setting:`STATICFILES_FINDERS` (by default, per-app ``static/`` subdirectories, |
| 49 | 49 | and any directories you include in :setting:`STATICFILES_DIRS`). Files from |
| 50 | | those locations will be collected into :setting:`STATICFILES_ROOT`. |
| | 50 | those locations will be collected into :setting:`STATIC_ROOT`. |
| 51 | 51 | |
| 52 | | .. setting:: STATICFILES_URL |
| | 52 | .. setting:: STATIC_URL |
| 53 | 53 | |
| 54 | | STATICFILES_URL |
| | 54 | STATIC_URL |
| 55 | 55 | --------------- |
| 56 | 56 | |
| 57 | 57 | Default: ``'/static/'`` |
| 58 | 58 | |
| 59 | | The URL that handles the files served from :setting:`STATICFILES_ROOT`, e.g.:: |
| | 59 | The URL that handles the files served from :setting:`STATIC_ROOT`, e.g.:: |
| 60 | 60 | |
| 61 | | STATICFILES_URL = '/site_media/static/' |
| | 61 | STATIC_URL = '/site_media/static/' |
| 62 | 62 | |
| 63 | 63 | ... or perhaps:: |
| 64 | 64 | |
| 65 | | STATICFILES_URL = 'http://static.example.com/' |
| | 65 | STATIC_URL = 'http://static.example.com/' |
| 66 | 66 | |
| 67 | 67 | This should **always** have a trailing slash. |
| 68 | 68 | |
| … |
… |
tuples, e.g.::
|
| 98 | 98 | |
| 99 | 99 | With this configuration, the :djadmin:`collectstatic` management command would |
| 100 | 100 | for example collect the stats files in a ``'downloads'`` directory. So |
| 101 | | assuming you have :setting:`STATICFILES_URL` set ``'/static/'``, this would |
| | 101 | assuming you have :setting:`STATIC_URL` set ``'/static/'``, this would |
| 102 | 102 | allow you to refer to the file ``'/opt/webfiles/stats/polls_20101022.tar.gz'`` |
| 103 | 103 | with ``'/static/downloads/polls_20101022.tar.gz'`` in your templates. |
| 104 | 104 | |
| … |
… |
collectstatic
|
| 160 | 160 | |
| 161 | 161 | .. django-admin:: collectstatic |
| 162 | 162 | |
| 163 | | Collects the static files into :setting:`STATICFILES_ROOT`. |
| | 163 | Collects the static files into :setting:`STATIC_ROOT`. |
| 164 | 164 | |
| 165 | 165 | Duplicate file names are by default resolved in a similar way to how template |
| 166 | 166 | resolution works: the file that is first found in one of the specified |
| … |
… |
collected for a given path.
|
| 223 | 223 | Other Helpers |
| 224 | 224 | ============= |
| 225 | 225 | |
| 226 | | The ``staticfiles`` context processor |
| 227 | | ------------------------------------- |
| | 226 | The ``static`` context processor |
| | 227 | -------------------------------- |
| 228 | 228 | |
| 229 | | .. function:: django.contrib.staticfiles.context_processors.staticfiles |
| | 229 | .. function:: django.contrib.staticfiles.context_processors.static |
| 230 | 230 | |
| 231 | | This context processor adds the :setting:`STATICFILES_URL` into each template |
| 232 | | context as the variable ``{{ STATICFILES_URL }}``. To use it, make sure that |
| 233 | | ``'django.contrib.staticfiles.context_processors.staticfiles'`` appears |
| | 231 | This context processor adds the :setting:`STATIC_URL` into each template |
| | 232 | context as the variable ``{{ STATIC_URL }}``. To use it, make sure that |
| | 233 | ``'django.contrib.staticfiles.context_processors.static'`` appears |
| 234 | 234 | somewhere in your :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting. |
| 235 | 235 | |
| 236 | 236 | Remember, only templates rendered with :class:`~django.template.RequestContext` |
| 237 | 237 | will have acces to the data provided by this (and any) context processor. |
| 238 | 238 | |
| 239 | | .. templatetag:: get_staticfiles_prefix |
| | 239 | .. templatetag:: get_static_prefix |
| 240 | 240 | |
| 241 | | The ``get_staticfiles_prefix`` templatetag |
| 242 | | ========================================== |
| | 241 | The ``get_static_prefix`` templatetag |
| | 242 | ===================================== |
| 243 | 243 | |
| 244 | 244 | .. highlight:: html+django |
| 245 | 245 | |
| 246 | 246 | If you're not using :class:`~django.template.RequestContext`, or if you need |
| 247 | | more control over exactly where and how :setting:`STATICFILES_URL` is injected |
| 248 | | into the template, you can use the :ttag:`get_staticfiles_prefix` template tag |
| | 247 | more control over exactly where and how :setting:`STATIC_URL` is injected |
| | 248 | into the template, you can use the :ttag:`get_static_prefix` template tag |
| 249 | 249 | instead:: |
| 250 | 250 | |
| 251 | 251 | {% load staticfiles %} |
| 252 | | <img src="{% get_staticfiles_prefix %}images/hi.jpg" /> |
| | 252 | <img src="{% get_static_prefix %}images/hi.jpg" /> |
| 253 | 253 | |
| 254 | 254 | There's also a second form you can use to avoid extra processing if you need |
| 255 | 255 | the value multiple times:: |
| 256 | 256 | |
| 257 | 257 | {% load staticfiles %} |
| 258 | | {% get_staticfiles_prefix as STATIC_PREFIX %} |
| | 258 | {% get_static_prefix as STATIC_PREFIX %} |
| 259 | 259 | |
| 260 | 260 | <img src="{{ STATIC_PREFIX }}images/hi.jpg" /> |
| 261 | 261 | <img src="{{ STATIC_PREFIX }}images/hi2.jpg" /> |
| … |
… |
primary URL configuration::
|
| 292 | 292 | ) |
| 293 | 293 | |
| 294 | 294 | Note, the begin of the pattern (``r'^static/'``) should be your |
| 295 | | :setting:`STATICFILES_URL` setting. |
| | 295 | :setting:`STATIC_URL` setting. |
| 296 | 296 | |
| 297 | 297 | Since this is a bit finicky, there's also a helper function that'll do this for you: |
| 298 | 298 | |
| 299 | | .. function:: django.contrib.staticfiles.urls.staticfiles_urlpatterns() |
| | 299 | .. function:: django.contrib.staticfiles.urls.static_urlpatterns() |
| 300 | 300 | |
| 301 | 301 | This will return the proper URL pattern for serving static files to your |
| 302 | 302 | already defined pattern list. Use it like this:: |
| 303 | 303 | |
| 304 | | from django.contrib.staticfiles.urls import staticfiles_urlpatterns |
| | 304 | from django.contrib.staticfiles.urls import static_urlpatterns |
| 305 | 305 | |
| 306 | 306 | # ... the rest of your URLconf here ... |
| 307 | 307 | |
| 308 | | urlpatterns += staticfiles_urlpatterns() |
| | 308 | urlpatterns += static_urlpatterns() |
| 309 | 309 | |
-
diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
index b394cd7..856e619 100644
|
a
|
b
|
administrative interface. Make sure to use a trailing slash, and to have this be
|
| 56 | 56 | different from the :setting:``MEDIA_URL`` setting (since the same URL cannot be |
| 57 | 57 | mapped onto two different sets of files). For integration with :doc:`staticfiles |
| 58 | 58 | </ref/contrib/staticfiles>`, this should be the same as |
| 59 | | :setting:`STATICFILES_URL` followed by ``'admin/'``. |
| | 59 | :setting:`STATIC_URL` followed by ``'admin/'``. |
| 60 | 60 | |
| 61 | 61 | .. setting:: ADMINS |
| 62 | 62 | |
-
diff --git a/docs/releases/1.3.txt b/docs/releases/1.3.txt
index b5c0035..f795e77 100644
|
a
|
b
|
In previous versions of Django, it was common to place static assets in
|
| 59 | 59 | app is to make it easier to keep static files separate from user-uploaded |
| 60 | 60 | files. For this reason, you will probably want to make your |
| 61 | 61 | :setting:`MEDIA_ROOT` and :setting:`MEDIA_URL` different from your |
| 62 | | :setting:`STATICFILES_ROOT` and :setting:`STATICFILES_URL`. You will need to |
| | 62 | :setting:`STATIC_ROOT` and :setting:`STATIC_URL`. You will need to |
| 63 | 63 | arrange for serving of files in :setting:`MEDIA_ROOT` yourself; |
| 64 | 64 | ``staticfiles`` does not deal with user-uploaded media at all. |
| 65 | 65 | |
-
diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
index 0a6c060..2d16983 100644
|
a
|
b
|
class StaticFilesTestCase(TestCase):
|
| 23 | 23 | Test case with a couple utility assertions. |
| 24 | 24 | """ |
| 25 | 25 | def setUp(self): |
| 26 | | self.old_staticfiles_url = settings.STATICFILES_URL |
| 27 | | self.old_staticfiles_root = settings.STATICFILES_ROOT |
| | 26 | self.old_static_url = settings.STATIC_URL |
| | 27 | self.old_static_root = settings.STATIC_ROOT |
| 28 | 28 | self.old_staticfiles_dirs = settings.STATICFILES_DIRS |
| 29 | 29 | self.old_staticfiles_finders = settings.STATICFILES_FINDERS |
| 30 | 30 | self.old_media_root = settings.MEDIA_ROOT |
| … |
… |
class StaticFilesTestCase(TestCase):
|
| 40 | 40 | settings.DEBUG = True |
| 41 | 41 | settings.MEDIA_ROOT = os.path.join(site_media, 'media') |
| 42 | 42 | settings.MEDIA_URL = '/media/' |
| 43 | | settings.STATICFILES_ROOT = os.path.join(site_media, 'static') |
| 44 | | settings.STATICFILES_URL = '/static/' |
| | 43 | settings.STATIC_ROOT = os.path.join(site_media, 'static') |
| | 44 | settings.STATIC_URL = '/static/' |
| 45 | 45 | settings.ADMIN_MEDIA_PREFIX = '/static/admin/' |
| 46 | 46 | settings.STATICFILES_DIRS = ( |
| 47 | 47 | os.path.join(TEST_ROOT, 'project', 'documents'), |
| … |
… |
class StaticFilesTestCase(TestCase):
|
| 52 | 52 | 'django.contrib.staticfiles.finders.DefaultStorageFinder', |
| 53 | 53 | ) |
| 54 | 54 | settings.INSTALLED_APPS = [ |
| | 55 | "django.contrib.staticfiles", |
| 55 | 56 | "regressiontests.staticfiles_tests", |
| 56 | 57 | ] |
| 57 | 58 | |
| … |
… |
class StaticFilesTestCase(TestCase):
|
| 65 | 66 | settings.MEDIA_ROOT = self.old_media_root |
| 66 | 67 | settings.MEDIA_URL = self.old_media_url |
| 67 | 68 | settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix |
| 68 | | settings.STATICFILES_ROOT = self.old_staticfiles_root |
| 69 | | settings.STATICFILES_URL = self.old_staticfiles_url |
| | 69 | settings.STATIC_ROOT = self.old_static_root |
| | 70 | settings.STATIC_URL = self.old_static_url |
| 70 | 71 | settings.STATICFILES_DIRS = self.old_staticfiles_dirs |
| 71 | 72 | settings.STATICFILES_FINDERS = self.old_staticfiles_finders |
| 72 | 73 | settings.INSTALLED_APPS = self.old_installed_apps |
| … |
… |
class BuildStaticTestCase(StaticFilesTestCase):
|
| 91 | 92 | def setUp(self): |
| 92 | 93 | super(BuildStaticTestCase, self).setUp() |
| 93 | 94 | self.old_staticfiles_storage = settings.STATICFILES_STORAGE |
| 94 | | self.old_root = settings.STATICFILES_ROOT |
| 95 | | settings.STATICFILES_ROOT = tempfile.mkdtemp() |
| | 95 | self.old_root = settings.STATIC_ROOT |
| | 96 | settings.STATIC_ROOT = tempfile.mkdtemp() |
| 96 | 97 | self.run_collectstatic() |
| 97 | 98 | |
| 98 | 99 | def tearDown(self): |
| 99 | | shutil.rmtree(settings.STATICFILES_ROOT) |
| 100 | | settings.STATICFILES_ROOT = self.old_root |
| | 100 | shutil.rmtree(settings.STATIC_ROOT) |
| | 101 | settings.STATIC_ROOT = self.old_root |
| 101 | 102 | super(BuildStaticTestCase, self).tearDown() |
| 102 | 103 | |
| 103 | 104 | def run_collectstatic(self, **kwargs): |
| … |
… |
class BuildStaticTestCase(StaticFilesTestCase):
|
| 106 | 107 | |
| 107 | 108 | def _get_file(self, filepath): |
| 108 | 109 | assert filepath, 'filepath is empty.' |
| 109 | | filepath = os.path.join(settings.STATICFILES_ROOT, filepath) |
| | 110 | filepath = os.path.join(settings.STATIC_ROOT, filepath) |
| 110 | 111 | f = open(filepath) |
| 111 | 112 | try: |
| 112 | 113 | return f.read() |
| … |
… |
class TestBuildStaticDryRun(BuildStaticTestCase):
|
| 231 | 232 | """ |
| 232 | 233 | With --dry-run, no files created in destination dir. |
| 233 | 234 | """ |
| 234 | | self.assertEquals(os.listdir(settings.STATICFILES_ROOT), []) |
| | 235 | self.assertEquals(os.listdir(settings.STATIC_ROOT), []) |
| 235 | 236 | |
| 236 | 237 | |
| 237 | 238 | if sys.platform != 'win32': |
| … |
… |
if sys.platform != 'win32':
|
| 251 | 252 | With ``--link``, symbolic links are created. |
| 252 | 253 | |
| 253 | 254 | """ |
| 254 | | self.failUnless(os.path.islink(os.path.join(settings.STATICFILES_ROOT, 'test.txt'))) |
| | 255 | self.failUnless(os.path.islink(os.path.join(settings.STATIC_ROOT, 'test.txt'))) |
| 255 | 256 | |
| 256 | 257 | |
| 257 | 258 | class TestServeStatic(StaticFilesTestCase): |
| … |
… |
class TestServeStatic(StaticFilesTestCase):
|
| 262 | 263 | |
| 263 | 264 | def _response(self, filepath): |
| 264 | 265 | return self.client.get( |
| 265 | | posixpath.join(settings.STATICFILES_URL, filepath)) |
| | 266 | posixpath.join(settings.STATIC_URL, filepath)) |
| 266 | 267 | |
| 267 | 268 | def assertFileContains(self, filepath, text): |
| 268 | 269 | self.assertContains(self._response(filepath), text) |
| … |
… |
class TestServeDisabled(TestServeStatic):
|
| 287 | 288 | |
| 288 | 289 | class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults): |
| 289 | 290 | """ |
| 290 | | Test static asset serving view with staticfiles_urlpatterns helper. |
| | 291 | Test static asset serving view with static_urlpatterns helper. |
| 291 | 292 | """ |
| 292 | 293 | pass |
| 293 | 294 | |
| 294 | 295 | class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults): |
| 295 | 296 | """ |
| 296 | | Test static asset serving view with staticfiles_urlpatterns helper. |
| | 297 | Test static asset serving view with static_urlpatterns helper. |
| 297 | 298 | """ |
| 298 | 299 | urls = "regressiontests.staticfiles_tests.urls.helper" |
| 299 | 300 | |
| … |
… |
class TestMiscFinder(TestCase):
|
| 374 | 375 | finders.get_finder, "foo.bar.FooBarFinder") |
| 375 | 376 | |
| 376 | 377 | |
| 377 | | class TemplateTagTest(TestCase): |
| 378 | | def test_get_staticfiles_prefix(self): |
| | 378 | class TemplateTagTest(StaticFilesTestCase): |
| | 379 | def test_get_static_prefix(self): |
| 379 | 380 | """ |
| 380 | | Test the get_staticfiles_prefix helper return the STATICFILES_URL setting. |
| | 381 | Test the get_static_prefix helper return the STATIC_URL setting. |
| 381 | 382 | """ |
| 382 | 383 | self.assertEquals(Template( |
| 383 | | "{% load staticfiles %}" |
| 384 | | "{% get_staticfiles_prefix %}" |
| 385 | | ).render(Context()), settings.STATICFILES_URL) |
| | 384 | "{% load static %}" |
| | 385 | "{% get_static_prefix %}" |
| | 386 | ).render(Context()), settings.STATIC_URL) |
| 386 | 387 | |
| 387 | | def test_get_staticfiles_prefix_with_as(self): |
| | 388 | def test_get_static_prefix_with_as(self): |
| 388 | 389 | """ |
| 389 | | Test the get_staticfiles_prefix helper return the STATICFILES_URL setting. |
| | 390 | Test the get_static_prefix helper return the STATIC_URL setting. |
| 390 | 391 | """ |
| 391 | 392 | self.assertEquals(Template( |
| 392 | | "{% load staticfiles %}" |
| 393 | | "{% get_staticfiles_prefix as staticfiles_prefix %}" |
| 394 | | "{{ staticfiles_prefix }}" |
| 395 | | ).render(Context()), settings.STATICFILES_URL) |
| | 393 | "{% load static %}" |
| | 394 | "{% get_static_prefix as static_prefix %}" |
| | 395 | "{{ static_prefix }}" |
| | 396 | ).render(Context()), settings.STATIC_URL) |
-
diff --git a/tests/regressiontests/staticfiles_tests/urls/helper.py b/tests/regressiontests/staticfiles_tests/urls/helper.py
index e4951d1..51ab63e 100644
|
a
|
b
|
|
| 1 | | from django.contrib.staticfiles.urls import staticfiles_urlpatterns |
| | 1 | from django.contrib.staticfiles.urls import static_urlpatterns |
| 2 | 2 | |
| 3 | | urlpatterns = staticfiles_urlpatterns() |
| | 3 | urlpatterns = static_urlpatterns() |