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:
Jannis Leidel 2010-10-21 03:16:41 +00:00
parent a28823e364
commit 1c4868f4c1
5 changed files with 31 additions and 8 deletions

View File

@ -6,7 +6,7 @@ from django.core.exceptions import ImproperlyConfigured
urlpatterns = []
# only serve non-fqdn URLs
if not settings.DEBUG:
if settings.DEBUG:
urlpatterns += patterns('',
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.
"""
if settings.DEBUG:
if not settings.DEBUG:
return []
if prefix is None:
prefix = settings.STATICFILES_URL

View File

@ -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
a template called ``static/directory_index.html``.
"""
if settings.DEBUG:
if not settings.DEBUG:
raise ImproperlyConfigured("The view to serve static files can only "
"be used if the DEBUG setting is True")
if not document_root:

View File

@ -29,11 +29,13 @@ class StaticFilesTestCase(TestCase):
self.old_media_root = settings.MEDIA_ROOT
self.old_media_url = settings.MEDIA_URL
self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
self.old_debug = settings.DEBUG
# We have to load these apps to test staticfiles.
load_app('regressiontests.staticfiles_tests.apps.test')
load_app('regressiontests.staticfiles_tests.apps.no_label')
site_media = os.path.join(TEST_ROOT, 'project', 'site_media')
settings.DEBUG = True
settings.MEDIA_ROOT = os.path.join(site_media, 'media')
settings.MEDIA_URL = '/media/'
settings.STATICFILES_ROOT = os.path.join(site_media, 'static')
@ -49,6 +51,7 @@ class StaticFilesTestCase(TestCase):
)
def tearDown(self):
settings.DEBUG = self.old_debug
settings.MEDIA_ROOT = self.old_media_root
settings.MEDIA_URL = self.old_media_url
settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix
@ -208,6 +211,8 @@ class TestServeStatic(StaticFilesTestCase):
"""
Test static asset serving view.
"""
urls = "regressiontests.staticfiles_tests.urls.default"
def _response(self, filepath):
return self.client.get(
posixpath.join(settings.STATICFILES_URL, filepath))
@ -219,12 +224,25 @@ class TestServeStatic(StaticFilesTestCase):
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):
"""
Test static asset serving view with staticfiles_urlpatterns helper.
"""
urls = "regressiontests.staticfiles_tests.urls.default"
pass
class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
"""

View File

@ -1,6 +1,7 @@
import mimetypes
from os import path
from django.conf import settings
from django.test import TestCase
from django.http import HttpResponseNotModified
from regressiontests.views.urls import media_dir
@ -8,6 +9,13 @@ from regressiontests.views.urls import media_dir
class StaticTests(TestCase):
"""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):
"The static view can serve static media"
media_files = ['file.txt', 'file.txt.gz']

View File

@ -41,7 +41,4 @@ urlpatterns = patterns('',
# special headers views
(r'special_headers/', include('regressiontests.special_headers.urls')),
# static files handling
(r'^', include('regressiontests.staticfiles_tests.urls.default')),
)