Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9005: don't wig out when reversing a URL if SETTINGS_M…

…ODULE isn'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. Backport of r10350 from trunk.

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

0 notes on commit a080fcd

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