Ticket #5742: static_patterns.diff
File static_patterns.diff, 3.4 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, 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
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_pattern`` 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_pattern``. 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 135 end 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 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 156 In 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.