Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4453 -- Allow dots in URL pattern names (although the string i…

…n that case is first tried as an import path and only then falls back to being treated as a pattern).

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5530 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a5802b35ddc599f8425b1759148ca2ebc5f32837 1 parent 14161db
Malcolm Tredinnick authored June 25, 2007
21  django/core/urlresolvers.py
@@ -27,11 +27,24 @@ class NoReverseMatch(Exception):
27 27
     # Don't make this raise an error when used in a template.
28 28
     silent_variable_failure = True
29 29
 
30  
-def get_callable(lookup_view):
  30
+def get_callable(lookup_view, can_fail=False):
  31
+    """
  32
+    Convert a string version of a function name to the callable object.
  33
+
  34
+    If the lookup_view is not an import path, it is assumed to be a URL pattern
  35
+    label and the original string is returned.
  36
+
  37
+    If can_fail is True, lookup_view might be a URL pattern label, so errors
  38
+    during the import fail and the string is returned.
  39
+    """
31 40
     if not callable(lookup_view):
32 41
         mod_name, func_name = get_mod_func(lookup_view)
33  
-        if func_name != '':
34  
-            lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
  42
+        try:
  43
+            if func_name != '':
  44
+                lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
  45
+        except (ImportError, AttributeError):
  46
+            if not can_fail:
  47
+                raise
35 48
     return lookup_view
36 49
 get_callable = memoize(get_callable, _callable_cache)
37 50
 
@@ -248,7 +261,7 @@ def resolve500(self):
248 261
 
249 262
     def reverse(self, lookup_view, *args, **kwargs):
250 263
         try:
251  
-            lookup_view = get_callable(lookup_view)
  264
+            lookup_view = get_callable(lookup_view, True)
252 265
         except (ImportError, AttributeError):
253 266
             raise NoReverseMatch
254 267
         if lookup_view in self.reverse_dict:
2  tests/regressiontests/templates/tests.py
@@ -725,7 +725,7 @@ def test_templates(self):
725 725
             'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),
726 726
             'url02' : ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
727 727
             'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'),
728  
-            'url04' : ('{% url named-client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
  728
+            'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
729 729
 
730 730
             # Failures
731 731
             'url-fail01' : ('{% url %}', {}, template.TemplateSyntaxError),
2  tests/regressiontests/templates/urls.py
@@ -7,5 +7,5 @@
7 7
     (r'^$', views.index),
8 8
     (r'^client/(\d+)/$', views.client),
9 9
     (r'^client/(\d+)/(?P<action>[^/]+)/$', views.client_action),
10  
-    url(r'^named-client/(\d+)/$', views.client, name="named-client"),
  10
+    url(r'^named-client/(\d+)/$', views.client, name="named.client"),
11 11
 )

0 notes on commit a5802b3

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