Fixed #14507 -- Corrected the logic of the URL helpers and view of staticfiles to actual work like documented (only when settings.DEBUG is True). Thanks for the report and initial patch, mbi.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14306 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a28823e364
commit
1c4868f4c1
|
@ -6,7 +6,7 @@ from django.core.exceptions import ImproperlyConfigured
|
||||||
urlpatterns = []
|
urlpatterns = []
|
||||||
|
|
||||||
# only serve non-fqdn URLs
|
# only serve non-fqdn URLs
|
||||||
if not settings.DEBUG:
|
if settings.DEBUG:
|
||||||
urlpatterns += patterns('',
|
urlpatterns += patterns('',
|
||||||
url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
|
url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
|
||||||
)
|
)
|
||||||
|
@ -15,7 +15,7 @@ def staticfiles_urlpatterns(prefix=None):
|
||||||
"""
|
"""
|
||||||
Helper function to return a URL pattern for serving static files.
|
Helper function to return a URL pattern for serving static files.
|
||||||
"""
|
"""
|
||||||
if settings.DEBUG:
|
if not settings.DEBUG:
|
||||||
return []
|
return []
|
||||||
if prefix is None:
|
if prefix is None:
|
||||||
prefix = settings.STATICFILES_URL
|
prefix = settings.STATICFILES_URL
|
||||||
|
|
|
@ -41,7 +41,7 @@ def serve(request, path, document_root=None, show_indexes=False):
|
||||||
template hardcoded below, but if you'd like to override it, you can create
|
template hardcoded below, but if you'd like to override it, you can create
|
||||||
a template called ``static/directory_index.html``.
|
a template called ``static/directory_index.html``.
|
||||||
"""
|
"""
|
||||||
if settings.DEBUG:
|
if not settings.DEBUG:
|
||||||
raise ImproperlyConfigured("The view to serve static files can only "
|
raise ImproperlyConfigured("The view to serve static files can only "
|
||||||
"be used if the DEBUG setting is True")
|
"be used if the DEBUG setting is True")
|
||||||
if not document_root:
|
if not document_root:
|
||||||
|
|
|
@ -29,11 +29,13 @@ class StaticFilesTestCase(TestCase):
|
||||||
self.old_media_root = settings.MEDIA_ROOT
|
self.old_media_root = settings.MEDIA_ROOT
|
||||||
self.old_media_url = settings.MEDIA_URL
|
self.old_media_url = settings.MEDIA_URL
|
||||||
self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
|
self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
|
||||||
|
self.old_debug = settings.DEBUG
|
||||||
|
|
||||||
# We have to load these apps to test staticfiles.
|
# We have to load these apps to test staticfiles.
|
||||||
load_app('regressiontests.staticfiles_tests.apps.test')
|
load_app('regressiontests.staticfiles_tests.apps.test')
|
||||||
load_app('regressiontests.staticfiles_tests.apps.no_label')
|
load_app('regressiontests.staticfiles_tests.apps.no_label')
|
||||||
site_media = os.path.join(TEST_ROOT, 'project', 'site_media')
|
site_media = os.path.join(TEST_ROOT, 'project', 'site_media')
|
||||||
|
settings.DEBUG = True
|
||||||
settings.MEDIA_ROOT = os.path.join(site_media, 'media')
|
settings.MEDIA_ROOT = os.path.join(site_media, 'media')
|
||||||
settings.MEDIA_URL = '/media/'
|
settings.MEDIA_URL = '/media/'
|
||||||
settings.STATICFILES_ROOT = os.path.join(site_media, 'static')
|
settings.STATICFILES_ROOT = os.path.join(site_media, 'static')
|
||||||
|
@ -49,6 +51,7 @@ class StaticFilesTestCase(TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
settings.DEBUG = self.old_debug
|
||||||
settings.MEDIA_ROOT = self.old_media_root
|
settings.MEDIA_ROOT = self.old_media_root
|
||||||
settings.MEDIA_URL = self.old_media_url
|
settings.MEDIA_URL = self.old_media_url
|
||||||
settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix
|
settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix
|
||||||
|
@ -208,6 +211,8 @@ class TestServeStatic(StaticFilesTestCase):
|
||||||
"""
|
"""
|
||||||
Test static asset serving view.
|
Test static asset serving view.
|
||||||
"""
|
"""
|
||||||
|
urls = "regressiontests.staticfiles_tests.urls.default"
|
||||||
|
|
||||||
def _response(self, filepath):
|
def _response(self, filepath):
|
||||||
return self.client.get(
|
return self.client.get(
|
||||||
posixpath.join(settings.STATICFILES_URL, filepath))
|
posixpath.join(settings.STATICFILES_URL, filepath))
|
||||||
|
@ -219,12 +224,25 @@ class TestServeStatic(StaticFilesTestCase):
|
||||||
self.assertEquals(self._response(filepath).status_code, 404)
|
self.assertEquals(self._response(filepath).status_code, 404)
|
||||||
|
|
||||||
|
|
||||||
|
class TestServeDisabled(TestServeStatic):
|
||||||
|
"""
|
||||||
|
Test serving media from django.contrib.admin.
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
super(TestServeDisabled, self).setUp()
|
||||||
|
settings.DEBUG = False
|
||||||
|
|
||||||
|
def test_disabled_serving(self):
|
||||||
|
self.assertRaisesRegexp(ImproperlyConfigured, "The view to serve "
|
||||||
|
"static files can only be used if the DEBUG setting is True",
|
||||||
|
self._response, 'test.txt')
|
||||||
|
|
||||||
|
|
||||||
class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults):
|
class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults):
|
||||||
"""
|
"""
|
||||||
Test static asset serving view with staticfiles_urlpatterns helper.
|
Test static asset serving view with staticfiles_urlpatterns helper.
|
||||||
"""
|
"""
|
||||||
urls = "regressiontests.staticfiles_tests.urls.default"
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
|
class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import mimetypes
|
import mimetypes
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.http import HttpResponseNotModified
|
from django.http import HttpResponseNotModified
|
||||||
from regressiontests.views.urls import media_dir
|
from regressiontests.views.urls import media_dir
|
||||||
|
@ -8,6 +9,13 @@ from regressiontests.views.urls import media_dir
|
||||||
class StaticTests(TestCase):
|
class StaticTests(TestCase):
|
||||||
"""Tests django views in django/views/static.py"""
|
"""Tests django views in django/views/static.py"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.old_debug = settings.DEBUG
|
||||||
|
settings.DEBUG = True
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
settings.DEBUG = self.old_debug
|
||||||
|
|
||||||
def test_serve(self):
|
def test_serve(self):
|
||||||
"The static view can serve static media"
|
"The static view can serve static media"
|
||||||
media_files = ['file.txt', 'file.txt.gz']
|
media_files = ['file.txt', 'file.txt.gz']
|
||||||
|
|
|
@ -41,7 +41,4 @@ urlpatterns = patterns('',
|
||||||
|
|
||||||
# special headers views
|
# special headers views
|
||||||
(r'special_headers/', include('regressiontests.special_headers.urls')),
|
(r'special_headers/', include('regressiontests.special_headers.urls')),
|
||||||
|
|
||||||
# static files handling
|
|
||||||
(r'^', include('regressiontests.staticfiles_tests.urls.default')),
|
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue