Ticket #5742: static_patterns.diff

File static_patterns.diff, 3.4 KB (added by Chris Beaven, 16 years ago)

Ability to test against any setting, not just DEBUG

  • django/conf/urls/defaults.py

     
    11from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
    22from django.core.exceptions import ImproperlyConfigured
    33
    4 __all__ = ['handler404', 'handler500', 'include', 'patterns', 'url']
     4__all__ = ['handler404', 'handler500', 'include', 'patterns', 'url',
     5           'static_pattern']
    56
    67handler404 = 'django.views.defaults.page_not_found'
    78handler500 = 'django.views.defaults.server_error'
     
    3031                view = prefix + '.' + view
    3132        return RegexURLPattern(regex, view, kwargs, name)
    3233
     34def static_pattern(url=None, root=None, name=None, kwargs=None,
     35                   if_setting='DEBUG'):
     36    from django.conf import settings
     37    from django.views.static import serve
     38    if if_setting and not getattr(settings, if_setting, None):
     39        return []
     40    # Default to using MEDIA_URL and MEDIA_ROOT from settings.
     41    if url is None:
     42        url = settings.MEDIA_URL
     43    if root is None:
     44        root = settings.MEDIA_ROOT
     45    # Don't add anything if either is blank
     46    if not url or not root:
     47        return []
     48    # Can't do a static pattern for a remote.
     49    if '://' in url or not url:
     50        return []
     51    # Remove any prepending '/' from the URL - we don't want it for the regex.
     52    regex = r'^%s(?P<path>.*)' % url.lstrip('/')
     53    kwargs = kwargs or {}
     54    kwargs.update({'document_root': root})
     55    return [RegexURLPattern(regex, serve, kwargs, name)]
  • docs/static_files.txt

     
    123123in your production settings file. But you should be doing that anyway.
    124124
    125125.. _DEBUG setting: ../settings/#debug
     126
     127``static_pattern`` shortcut
     128===========================
     129
     130To make this even simpler, a shortcut is also imported along with the other
     131``django.conf.urls.defaults`` called ``static_pattern``.
     132
     133The most common usage, serving files from ``MEDIA_ROOT`` to ``MEDIA_URL`` when
     134``DEBUG`` is ``True``, can be achieved by simply adding the following to the
     135end of your URLconf::
     136
     137    urlpatterns += static_pattern()
     138
     139You can specify options to ``static_pattern`` to customize it's usage:
     140
     141    ===============  ==========================================================
     142    Argument         Description
     143    ===============  ==========================================================
     144    ``url``          The URL which files are served to. Defaults to
     145                     ``MEDIA_URL``.
     146    ``root``         The root of the filesystem to serve files from. Defaults
     147                     to ``MEDIA_ROOT``.
     148    ``name``         Name the URL pattern.
     149    ``kwargs``       A dictionary of extra arguments (such as ``show_indexes``)
     150                     to pass to the ``serve`` view.
     151    ``if_setting``   If not ``None``, only serves the files if the setting
     152                     exists in the ``settings`` module and is ``True``.
     153                     Defaults to ``'DEBUG'``.
     154    ===============  ==========================================================
     155
     156In addition to the ``if_setting`` argument test, files will not get served if
     157``url`` or ``root`` are empty, or if ``url`` is a remote URL.
Back to Top