Ticket #5742: static_patterns.2.patch

File static_patterns.2.patch, 3.3 KB (added by Chris Beaven, 17 years ago)

Changing the doc title to reflect the fact it's static_pattern, not static_patterns

  • 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, debug_only=True):
     35    from django.conf import settings
     36    from django.views.static import serve
     37    if debug_only and not settings.DEBUG:
     38        return []
     39    # Default to using MEDIA_URL and MEDIA_ROOT from settings.
     40    if url is None:
     41        url = settings.MEDIA_URL
     42    if root is None:
     43        root = settings.MEDIA_ROOT
     44    # Don't add anything if either is blank
     45    if not url or not root:
     46        return []
     47    # Can't do a static pattern for a remote.
     48    if '://' in url or not url:
     49        return []
     50    # Remove any prepending '/' from the URL - we don't want it for the regex.
     51    regex = r'^%s(?P<path>.*)' % url.lstrip('/')
     52    kwargs = kwargs or {}
     53    kwargs.update({'document_root': root})
     54    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_patterns``.
     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 end
     135of 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 to
     147                     ``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    ``debug_only``   If ``True``, only serves the files if ``DEBUG`` is
     152                     ``True``. Defaults to ``True``.
     153    ===============  ===========================================================
     154
     155In addition to the ``debug_only`` argument test, files will not get served if
     156``url`` or ``root`` are empty, or if ``url`` is a remote URL.
Back to Top