1
0
mirror of https://github.com/django/django.git synced 2025-07-04 01:39:20 +00:00

unicode: Added support for non-ASCII labels for URL patterns.

git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5585 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-07-02 12:24:58 +00:00
parent 9bf4bacf13
commit fc8bdc1af4
3 changed files with 18 additions and 9 deletions

View File

@ -38,14 +38,20 @@ def get_callable(lookup_view, can_fail=False):
If can_fail is True, lookup_view might be a URL pattern label, so errors
during the import fail and the string is returned.
"""
if not callable(lookup_view):
mod_name, func_name = get_mod_func(lookup_view)
try:
if func_name != '':
lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
except (ImportError, AttributeError):
if not can_fail:
raise
try:
# Bail out early if lookup_view is not ASCII. This can't be a function.
lookup_view = lookup_view.encode('ascii')
if not callable(lookup_view):
mod_name, func_name = get_mod_func(lookup_view)
try:
if func_name != '':
lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
except (ImportError, AttributeError):
if not can_fail:
raise
except UnicodeEncodeError:
pass
return lookup_view
get_callable = memoize(get_callable, _callable_cache)
@ -266,7 +272,7 @@ class RegexURLResolver(object):
except (ImportError, AttributeError):
raise NoReverseMatch
if lookup_view in self.reverse_dict:
return ''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]])
return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dict[lookup_view]])
raise NoReverseMatch
def reverse_helper(self, lookup_view, *args, **kwargs):

View File

@ -735,6 +735,7 @@ class Templates(unittest.TestCase):
'url02' : ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'),
'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
'url05' : (u'{% url метка_оператора 1 %}', {}, '/url_tag/unicode/1/'),
# Failures
'url-fail01' : ('{% url %}', {}, template.TemplateSyntaxError),

View File

@ -1,3 +1,4 @@
# coding: utf-8
from django.conf.urls.defaults import *
from regressiontests.templates import views
@ -8,4 +9,5 @@ urlpatterns = patterns('',
(r'^client/(\d+)/$', views.client),
(r'^client/(\d+)/(?P<action>[^/]+)/$', views.client_action),
url(r'^named-client/(\d+)/$', views.client, name="named.client"),
url(r'^unicode/(\d+)/$', views.client, name=u"метка_оператора"),
)