Code

Ticket #9005: 9005-with-tests.diff

File 9005-with-tests.diff, 3.2 KB (added by ericholscher, 5 years ago)

Patch with tests. Fails on trunk with above-mentioned error, passed with patch with correct lack of url resolution.

Line 
1diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
2index 4b40009..151b94e 100644
3--- a/django/core/management/__init__.py
4+++ b/django/core/management/__init__.py
5@@ -323,7 +323,7 @@ def setup_environ(settings_mod, original_settings_path=None):
6     else:
7         os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
8 
9-    # Import the project module. We add the parent directory to PYTHONPATH to
10+    # Import the project module. We add the parent directory to PYTHONPATH to
11     # avoid some of the path errors new users can have.
12     sys.path.append(os.path.join(project_directory, os.pardir))
13     project_module = import_module(project_name)
14diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
15index c08ecc4..e5b2101 100644
16--- a/django/template/defaulttags.py
17+++ b/django/template/defaulttags.py
18@@ -371,13 +371,18 @@ class URLNode(Node):
19         try:
20             url = reverse(self.view_name, args=args, kwargs=kwargs)
21         except NoReverseMatch:
22-            project_name = settings.SETTINGS_MODULE.split('.')[0]
23-            try:
24-                url = reverse(project_name + '.' + self.view_name,
25-                              args=args, kwargs=kwargs)
26-            except NoReverseMatch:
27+            if settings.SETTINGS_MODULE is None:
28+                # SETTINGS_MODULE is not set, so we can't guess the "main" app.
29                 if self.asvar is None:
30                     raise
31+            else:
32+                project_name = settings.SETTINGS_MODULE.split('.')[0]
33+                try:
34+                    url = reverse(project_name + '.' + self.view_name,
35+                              args=args, kwargs=kwargs)
36+                except NoReverseMatch:
37+                    if self.asvar is None:
38+                        raise
39 
40         if self.asvar:
41             context[self.asvar] = url
42diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
43index c511222..c45daf3 100644
44--- a/tests/regressiontests/templates/tests.py
45+++ b/tests/regressiontests/templates/tests.py
46@@ -152,6 +152,21 @@ class Templates(unittest.TestCase):
47         split = token.split_contents()
48         self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
49 
50+    def test_url_reverse_no_settings_module(self):
51+        #Regression test for #9005
52+        from django.template import Template, Context, TemplateSyntaxError
53+        old_settings_module = settings.SETTINGS_MODULE
54+        settings.SETTINGS_MODULE = None
55+        t = Template('{% url will_not_match %}')
56+        c = Context()
57+        try:
58+            rendered = t.render(c)
59+        except TemplateSyntaxError, e:
60+            #Assert that we are getting the template syntax error and not the
61+            #string encoding error.
62+            self.assertEquals(e.message, "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
63+        settings.SETTINGS_MODULE = old_settings_module
64+
65     def test_templates(self):
66         template_tests = self.get_template_tests()
67         filter_tests = filters.get_filter_tests()