Ticket #5742: static_patterns.patch
File static_patterns.patch, 3.3 KB (added by , 17 years ago) |
---|
-
django/conf/urls/defaults.py
1 1 from django.core.urlresolvers import RegexURLPattern, RegexURLResolver 2 2 from django.core.exceptions import ImproperlyConfigured 3 3 4 __all__ = ['handler404', 'handler500', 'include', 'patterns', 'url'] 4 __all__ = ['handler404', 'handler500', 'include', 'patterns', 'url', 5 'static_pattern'] 5 6 6 7 handler404 = 'django.views.defaults.page_not_found' 7 8 handler500 = 'django.views.defaults.server_error' … … 30 31 view = prefix + '.' + view 31 32 return RegexURLPattern(regex, view, kwargs, name) 32 33 34 def 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
123 123 in your production settings file. But you should be doing that anyway. 124 124 125 125 .. _DEBUG setting: ../settings/#debug 126 127 ``static_patterns`` shortcut 128 ============================ 129 130 To make this even simpler, a shortcut is also imported along with the other 131 ``django.conf.urls.defaults`` called ``static_patterns``. 132 133 The 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 135 of your URLconf:: 136 137 urlpatterns += static_pattern() 138 139 You 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 155 In 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.