Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jannis Leidel authored October 21, 2010
4  django/contrib/staticfiles/urls.py
@@ -6,7 +6,7 @@
6 6
 urlpatterns = []
7 7
 
8 8
 # only serve non-fqdn URLs
9  
-if not settings.DEBUG:
  9
+if settings.DEBUG:
10 10
     urlpatterns += patterns('',
11 11
         url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
12 12
     )
@@ -15,7 +15,7 @@ def staticfiles_urlpatterns(prefix=None):
15 15
     """
16 16
     Helper function to return a URL pattern for serving static files.
17 17
     """
18  
-    if settings.DEBUG:
  18
+    if not settings.DEBUG:
19 19
         return []
20 20
     if prefix is None:
21 21
         prefix = settings.STATICFILES_URL
2  django/contrib/staticfiles/views.py
@@ -41,7 +41,7 @@ def serve(request, path, document_root=None, show_indexes=False):
41 41
     template hardcoded below, but if you'd like to override it, you can create
42 42
     a template called ``static/directory_index.html``.
43 43
     """
44  
-    if settings.DEBUG:
  44
+    if not settings.DEBUG:
45 45
         raise ImproperlyConfigured("The view to serve static files can only "
46 46
                                    "be used if the DEBUG setting is True")
47 47
     if not document_root:
22  tests/regressiontests/staticfiles_tests/tests.py
@@ -29,11 +29,13 @@ def setUp(self):
29 29
         self.old_media_root = settings.MEDIA_ROOT
30 30
         self.old_media_url = settings.MEDIA_URL
31 31
         self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
  32
+        self.old_debug = settings.DEBUG
32 33
 
33 34
         # We have to load these apps to test staticfiles.
34 35
         load_app('regressiontests.staticfiles_tests.apps.test')
35 36
         load_app('regressiontests.staticfiles_tests.apps.no_label')
36 37
         site_media = os.path.join(TEST_ROOT, 'project', 'site_media')
  38
+        settings.DEBUG = True
37 39
         settings.MEDIA_ROOT =  os.path.join(site_media, 'media')
38 40
         settings.MEDIA_URL = '/media/'
39 41
         settings.STATICFILES_ROOT = os.path.join(site_media, 'static')
@@ -49,6 +51,7 @@ def setUp(self):
49 51
         )
50 52
 
51 53
     def tearDown(self):
  54
+        settings.DEBUG = self.old_debug
52 55
         settings.MEDIA_ROOT = self.old_media_root
53 56
         settings.MEDIA_URL = self.old_media_url
54 57
         settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix
@@ -208,6 +211,8 @@ class TestServeStatic(StaticFilesTestCase):
208 211
     """
209 212
     Test static asset serving view.
210 213
     """
  214
+    urls = "regressiontests.staticfiles_tests.urls.default"
  215
+
211 216
     def _response(self, filepath):
212 217
         return self.client.get(
213 218
             posixpath.join(settings.STATICFILES_URL, filepath))
@@ -219,12 +224,25 @@ def assertFileNotFound(self, filepath):
219 224
         self.assertEquals(self._response(filepath).status_code, 404)
220 225
 
221 226
 
  227
+class TestServeDisabled(TestServeStatic):
  228
+    """
  229
+    Test serving media from django.contrib.admin.
  230
+    """
  231
+    def setUp(self):
  232
+        super(TestServeDisabled, self).setUp()
  233
+        settings.DEBUG = False
  234
+
  235
+    def test_disabled_serving(self):
  236
+        self.assertRaisesRegexp(ImproperlyConfigured, "The view to serve "
  237
+            "static files can only be used if the DEBUG setting is True",
  238
+            self._response, 'test.txt')
  239
+
  240
+
222 241
 class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults):
223 242
     """
224 243
     Test static asset serving view with staticfiles_urlpatterns helper.
225 244
     """
226  
-    urls = "regressiontests.staticfiles_tests.urls.default"
227  
-
  245
+    pass
228 246
 
229 247
 class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
230 248
     """
8  tests/regressiontests/views/tests/static.py
... ...
@@ -1,6 +1,7 @@
1 1
 import mimetypes
2 2
 from os import path
3 3
 
  4
+from django.conf import settings
4 5
 from django.test import TestCase
5 6
 from django.http import HttpResponseNotModified
6 7
 from regressiontests.views.urls import media_dir
@@ -8,6 +9,13 @@
8 9
 class StaticTests(TestCase):
9 10
     """Tests django views in django/views/static.py"""
10 11
 
  12
+    def setUp(self):
  13
+        self.old_debug = settings.DEBUG
  14
+        settings.DEBUG = True
  15
+
  16
+    def tearDown(self):
  17
+        settings.DEBUG = self.old_debug
  18
+
11 19
     def test_serve(self):
12 20
         "The static view can serve static media"
13 21
         media_files = ['file.txt', 'file.txt.gz']
3  tests/urls.py
@@ -41,7 +41,4 @@
41 41
 
42 42
     # special headers views
43 43
     (r'special_headers/', include('regressiontests.special_headers.urls')),
44  
-
45  
-    # static files handling
46  
-    (r'^', include('regressiontests.staticfiles_tests.urls.default')),
47 44
 )

0 notes on commit 1c4868f

Please sign in to comment.
Something went wrong with that request. Please try again.