Author: jacob
Date: 2009-04-01 17:46:46 -0500 (Wed, 01 Apr 2009)
New Revision: 10350

Modified:
   django/trunk/django/core/management/__init__.py
   django/trunk/django/template/defaulttags.py
   django/trunk/tests/regressiontests/templates/tests.py
Log:
Fixed #9005: don't wig out when reversing a URL if SETTINGS_MODULE 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

Modified: django/trunk/django/core/management/__init__.py
===================================================================
--- django/trunk/django/core/management/__init__.py     2009-04-01 22:35:41 UTC 
(rev 10349)
+++ django/trunk/django/core/management/__init__.py     2009-04-01 22:46:46 UTC 
(rev 10350)
@@ -323,7 +323,7 @@
     else:
         os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, 
settings_name)
 
-    # Import the project module. We add the parent directory to PYTHONPATH to 
+    # Import the project module. We add the parent directory to PYTHONPATH to
     # avoid some of the path errors new users can have.
     sys.path.append(os.path.join(project_directory, os.pardir))
     project_module = import_module(project_name)

Modified: django/trunk/django/template/defaulttags.py
===================================================================
--- django/trunk/django/template/defaulttags.py 2009-04-01 22:35:41 UTC (rev 
10349)
+++ django/trunk/django/template/defaulttags.py 2009-04-01 22:46:46 UTC (rev 
10350)
@@ -370,14 +370,21 @@
         url = ''
         try:
             url = reverse(self.view_name, args=args, kwargs=kwargs)
-        except NoReverseMatch:
-            project_name = settings.SETTINGS_MODULE.split('.')[0]
-            try:
-                url = reverse(project_name + '.' + self.view_name,
+        except NoReverseMatch, e:
+            if settings.SETTINGS_MODULE:
+                project_name = settings.SETTINGS_MODULE.split('.')[0]
+                try:
+                    url = reverse(project_name + '.' + self.view_name,
                               args=args, kwargs=kwargs)
-            except NoReverseMatch:
+                except NoReverseMatch:
+                    if self.asvar is None:
+                        # Re-raise the original exception, not the one with
+                        # the path relative to the project. This makes a 
+                        # better error message.
+                        raise e
+            else:
                 if self.asvar is None:
-                    raise
+                    raise e
 
         if self.asvar:
             context[self.asvar] = url

Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py       2009-04-01 
22:35:41 UTC (rev 10349)
+++ django/trunk/tests/regressiontests/templates/tests.py       2009-04-01 
22:46:46 UTC (rev 10350)
@@ -153,6 +153,21 @@
         split = token.split_contents()
         self.assertEqual(split, ["sometag", '_("Page not found")', 
'value|yesno:_("yes,no")'])
 
+    def test_url_reverse_no_settings_module(self):
+        #Regression test for #9005
+        from django.template import Template, Context, TemplateSyntaxError
+        old_settings_module = settings.SETTINGS_MODULE
+        settings.SETTINGS_MODULE = None
+        t = Template('{% url will_not_match %}')
+        c = Context()
+        try:
+            rendered = t.render(c)
+        except TemplateSyntaxError, e:
+            #Assert that we are getting the template syntax error and not the
+            #string encoding error.
+            self.assertEquals(e.message, "Caught an exception while rendering: 
Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not 
found.")
+        settings.SETTINGS_MODULE = old_settings_module
+
     def test_templates(self):
         template_tests = self.get_template_tests()
         filter_tests = filters.get_filter_tests()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to