Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9005: don't wig out when reversing a URL if SETTINGS_MODULE is…

…n't set. While I was there, I fixed #10599 by re-raising the original error message, which is almost always a better idea. Thanks, Eric

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10350 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 624caace17dbb9e22f5a4c6c08937d68cbdbb066 1 parent dc832d7
Jacob Kaplan-Moss authored April 01, 2009
2  django/core/management/__init__.py
@@ -323,7 +323,7 @@ def setup_environ(settings_mod, original_settings_path=None):
323 323
     else:
324 324
         os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
325 325
 
326  
-    # Import the project module. We add the parent directory to PYTHONPATH to 
  326
+    # Import the project module. We add the parent directory to PYTHONPATH to
327 327
     # avoid some of the path errors new users can have.
328 328
     sys.path.append(os.path.join(project_directory, os.pardir))
329 329
     project_module = import_module(project_name)
19  django/template/defaulttags.py
@@ -370,14 +370,21 @@ def render(self, context):
370 370
         url = ''
371 371
         try:
372 372
             url = reverse(self.view_name, args=args, kwargs=kwargs)
373  
-        except NoReverseMatch:
374  
-            project_name = settings.SETTINGS_MODULE.split('.')[0]
375  
-            try:
376  
-                url = reverse(project_name + '.' + self.view_name,
  373
+        except NoReverseMatch, e:
  374
+            if settings.SETTINGS_MODULE:
  375
+                project_name = settings.SETTINGS_MODULE.split('.')[0]
  376
+                try:
  377
+                    url = reverse(project_name + '.' + self.view_name,
377 378
                               args=args, kwargs=kwargs)
378  
-            except NoReverseMatch:
  379
+                except NoReverseMatch:
  380
+                    if self.asvar is None:
  381
+                        # Re-raise the original exception, not the one with
  382
+                        # the path relative to the project. This makes a 
  383
+                        # better error message.
  384
+                        raise e
  385
+            else:
379 386
                 if self.asvar is None:
380  
-                    raise
  387
+                    raise e
381 388
 
382 389
         if self.asvar:
383 390
             context[self.asvar] = url
15  tests/regressiontests/templates/tests.py
@@ -153,6 +153,21 @@ def test_token_smart_split(self):
153 153
         split = token.split_contents()
154 154
         self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
155 155
 
  156
+    def test_url_reverse_no_settings_module(self):
  157
+        #Regression test for #9005
  158
+        from django.template import Template, Context, TemplateSyntaxError
  159
+        old_settings_module = settings.SETTINGS_MODULE
  160
+        settings.SETTINGS_MODULE = None
  161
+        t = Template('{% url will_not_match %}')
  162
+        c = Context()
  163
+        try:
  164
+            rendered = t.render(c)
  165
+        except TemplateSyntaxError, e:
  166
+            #Assert that we are getting the template syntax error and not the
  167
+            #string encoding error.
  168
+            self.assertEquals(e.message, "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
  169
+        settings.SETTINGS_MODULE = old_settings_module
  170
+
156 171
     def test_templates(self):
157 172
         template_tests = self.get_template_tests()
158 173
         filter_tests = filters.get_filter_tests()

0 notes on commit 624caac

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