Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #14507 -- Corrected the logic of the URL helpers and view of st…

…aticfiles 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
  • Loading branch information...
commit 1c4868f4c15f41c7840129d0f04c87f65aa94367 1 parent a28823e
@jezdez jezdez authored
View
4 django/contrib/staticfiles/urls.py
@@ -6,7 +6,7 @@
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
2  django/contrib/staticfiles/views.py
@@ -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
22 tests/regressiontests/staticfiles_tests/tests.py
@@ -29,11 +29,13 @@ def setUp(self):
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 @@ def setUp(self):
)
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 @@ def assertFileNotFound(self, filepath):
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
8 tests/regressiontests/views/tests/static.py
@@ -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 @@
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
3  tests/urls.py
@@ -41,7 +41,4 @@
# special headers views
(r'special_headers/', include('regressiontests.special_headers.urls')),
-
- # static files handling
- (r'^', include('regressiontests.staticfiles_tests.urls.default')),
)
Please sign in to comment.
Something went wrong with that request. Please try again.