Ticket #5408: 5408.patch
File 5408.patch, 6.3 KB (added by , 17 years ago) |
---|
-
django/template/defaulttags.py
331 331 return self.mapping.get(self.tagtype, '') 332 332 333 333 class URLNode(Node): 334 def __init__(self, view_ name, args, kwargs):335 self.view_ name = view_name334 def __init__(self, view_var_or_name, args, kwargs): 335 self.view_var_or_name = view_var_or_name 336 336 self.args = args 337 337 self.kwargs = kwargs 338 338 … … 340 340 from django.core.urlresolvers import reverse, NoReverseMatch 341 341 args = [arg.resolve(context) for arg in self.args] 342 342 kwargs = dict([(smart_str(k,'ascii'), v.resolve(context)) for k, v in self.kwargs.items()]) 343 view_name = resolve_variable(self.view_var_or_name, context) 343 344 try: 344 return reverse( self.view_name, args=args, kwargs=kwargs)345 return reverse(view_name, args=args, kwargs=kwargs) 345 346 except NoReverseMatch: 346 347 try: 347 348 project_name = settings.SETTINGS_MODULE.split('.')[0] 348 return reverse(project_name + '.' + self.view_name, args=args, kwargs=kwargs)349 return reverse(project_name + '.' + view_name, args=args, kwargs=kwargs) 349 350 except NoReverseMatch: 350 351 return '' 351 352 … … 954 955 955 956 This is a way to define links that aren't tied to a particular URL configuration:: 956 957 957 {% url path.to.some_view arg1,arg2,name1=value1 %} 958 {% url "path.to.some_view" arg1,arg2,name1=value1 %} 959 {% url path_variable arg1,arg2,name1=value1 %} 958 960 959 961 The first argument is a path to a view. It can be an absolute python path 960 962 or just ``app_name.view_name`` without the project name if the view is -
tests/regressiontests/templates/tests.py
783 783 784 784 ### URL TAG ######################################################## 785 785 # Successes 786 'url01' : ('{% url regressiontests.templates.views.clientclient.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),787 'url02' : ('{% url regressiontests.templates.views.client_actionclient.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),788 'url03' : ('{% url regressiontests.templates.views.index%}', {}, '/url_tag/'),789 'url04' : ('{% url named.clientclient.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),790 'url05' : (u'{% url метка_оператораv %}', {'v': u'Ω'},786 'url01' : ('{% url "regressiontests.templates.views.client" client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'), 787 'url02' : ('{% url "regressiontests.templates.views.client_action" client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), 788 'url03' : ('{% url "regressiontests.templates.views.index" %}', {}, '/url_tag/'), 789 'url04' : ('{% url "named.client" client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'), 790 'url05' : (u'{% url "метка_оператора" v %}', {'v': u'Ω'}, 791 791 '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), 792 'url06' : ('{% url view_path client.id %}', {'view_path': "regressiontests.templates.views.client", 'client': {'id': 1}}, '/url_tag/client/1/'), 793 'url07' : ('{% url view_path client.id, action="update" %}', {'view_path': "regressiontests.templates.views.client_action", 'client': {'id': 1}}, '/url_tag/client/1/update/'), 794 'url08' : ('{% url view_path %}', {'view_path': "regressiontests.templates.views.index", }, '/url_tag/'), 795 'url09' : ('{% url view_path client.id %}', {'view_path': "named.client", 'client': {'id': 1}}, '/url_tag/named-client/1/'), 796 'url10' : (u'{% url view_path v %}', {'view_path': "метка_оператора", 'v': u'Ω'}, 797 '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), 792 798 793 799 # Failures 794 800 'url-fail01' : ('{% url %}', {}, template.TemplateSyntaxError), 795 'url-fail02' : ('{% url no_such_view %}', {}, ''), 796 'url-fail03' : ('{% url regressiontests.templates.views.client no_such_param="value" %}', {}, ''), 801 'url-fail02' : ('{% url "no_such_view" %}', {}, ''), 802 'url-fail03' : ('{% url "regressiontests.templates.views.client" no_such_param="value" %}', {}, ''), 803 'url-fail04' : ('{% url view_path %}', {'view_path': "no_such_view"}, ''), 804 'url-fail05' : ('{% url view_path no_such_param="value" %}', {'view_path': "regressiontests.templates.views.client"}, ''), 797 805 } 798 806 799 807 # Register our custom template loader. -
docs/templates.txt
924 924 view function and optional parameters. This is a way to output links without 925 925 violating the DRY principle by having to hard-code URLs in your templates:: 926 926 927 {% url path.to.some_view arg1,arg2,name1=value1 %} 927 {% url "path.to.some_view" arg1,arg2,name1=value1 %} 928 {% url path_in_a_var arg1,arg2,name1=value1 %} 928 929 929 930 The first argument is a path to a view function in the format 930 ``package.package.module.function``. Additional arguments are optional and 931 ``package.package.module.function`` (either as a string literal with quotes or 932 the name of a variable containing the path). Additional arguments are optional and 931 933 should be comma-separated values that will be used as positional and keyword 932 934 arguments in the URL. All arguments required by the URLconf should be present. 933 935 … … 950 952 951 953 **New in development version:** If you're using `named URL patterns`_, 952 954 you can refer to the name of the pattern in the ``url`` tag instead of 953 using the path to the view. 955 using the path to the view. The fact that you can use a variable for the 956 path is new. Previous versions used the path directly but without the quotes, 957 which means any template written using this convention will break. 954 958 955 959 .. _named URL patterns: ../url_dispatch/#naming-url-patterns 956 960