Ticket #8994: 8994.patch
File 8994.patch, 3.3 KB (added by , 16 years ago) |
---|
-
docs/ref/templates/builtins.txt
a b The template tag will output the string ``/clients/client/123/``. 676 676 **New in development version:** If you're using :ref:`named URL patterns 677 677 <naming-url-patterns>`, you can refer to the name of the pattern in the ``url`` 678 678 tag instead of using the path to the view. 679 If you need to save the url to a context variable instead of inserting the url 680 where the tag is use the syntax:: 681 682 {% url path.to.some_view arg1,arg2,name1=value1 %} 683 684 This is useful for translated text which cannot contain tags, so you need to use 685 variables instead. 679 686 680 687 .. templatetag:: widthratio 681 688 -
django/template/defaulttags.py
a b class TemplateTagNode(Node): 351 351 return self.mapping.get(self.tagtype, '') 352 352 353 353 class URLNode(Node): 354 def __init__(self, view_name, args, kwargs):354 def __init__(self, view_name, var_name, args, kwargs): 355 355 self.view_name = view_name 356 self.var_name = var_name 356 357 self.args = args 357 358 self.kwargs = kwargs 358 359 … … class URLNode(Node): 362 363 kwargs = dict([(smart_str(k,'ascii'), v.resolve(context)) 363 364 for k, v in self.kwargs.items()]) 364 365 try: 365 r eturnreverse(self.view_name, args=args, kwargs=kwargs)366 r = reverse(self.view_name, args=args, kwargs=kwargs) 366 367 except NoReverseMatch: 367 368 project_name = settings.SETTINGS_MODULE.split('.')[0] 368 r eturnreverse(project_name + '.' + self.view_name,369 r = reverse(project_name + '.' + self.view_name, 369 370 args=args, kwargs=kwargs) 371 if self.var_name: 372 context[self.var_name] = r 373 return '' 374 else: 375 return r 370 376 371 377 class WidthRatioNode(Node): 372 378 def __init__(self, val_expr, max_expr, max_width): … … def templatetag(parser, token): 1010 1016 return TemplateTagNode(tag) 1011 1017 templatetag = register.tag(templatetag) 1012 1018 1019 #@register.url 1013 1020 def url(parser, token): 1014 1021 """ 1015 1022 Returns an absolute URL matching given view with its parameters. … … def url(parser, token): 1040 1047 {% url app_name.client client.id %} 1041 1048 1042 1049 The URL will look like ``/clients/client/123/``. 1050 1051 If you need to save the url to a context variable instead of inserting the url 1052 where the tag is use the syntax:: 1053 1054 {% url path.to.some_view arg1,arg2,name1=value1 %} 1055 1056 This is useful for translated text which cannot contain tags, so you need to use 1057 variables instead. 1043 1058 """ 1044 bits = token.contents.split(' ' , 2)1059 bits = token.contents.split(' ') 1045 1060 if len(bits) < 2: 1046 1061 raise TemplateSyntaxError("'%s' takes at least one argument" 1047 1062 " (path to a view)" % bits[0]) 1063 if bits[-2] == "as": 1064 var_name = bits[-1] 1065 bits = bits[:-2] 1066 else: 1067 var_name = None 1068 print bits 1048 1069 args = [] 1049 1070 kwargs = {} 1050 1071 if len(bits) > 2: … … def url(parser, token): 1055 1076 kwargs[k] = parser.compile_filter(v) 1056 1077 else: 1057 1078 args.append(parser.compile_filter(arg)) 1058 return URLNode(bits[1], args, kwargs)1079 return URLNode(bits[1],var_name, args, kwargs) 1059 1080 url = register.tag(url) 1060 1081 1061 1082 #@register.tag