Ticket #8994: 8994.patch

File 8994.patch, 3.3 KB (added by Henrik Vendelbo, 16 years ago)

docs,comments,code

  • docs/ref/templates/builtins.txt

    a b The template tag will output the string ``/clients/client/123/``.  
    676676**New in development version:** If you're using :ref:`named URL patterns
    677677<naming-url-patterns>`, you can refer to the name of the pattern in the ``url``
    678678tag instead of using the path to the view.
     679If you need to save the url to a context variable instead of inserting the url
     680where the tag is use the syntax::
     681
     682    {% url path.to.some_view arg1,arg2,name1=value1 %}
     683
     684This is useful for translated text which cannot contain tags, so you need to use
     685variables instead.
    679686
    680687.. templatetag:: widthratio
    681688
  • django/template/defaulttags.py

    a b class TemplateTagNode(Node):  
    351351        return self.mapping.get(self.tagtype, '')
    352352
    353353class URLNode(Node):
    354     def __init__(self, view_name, args, kwargs):
     354    def __init__(self, view_name, var_name, args, kwargs):
    355355        self.view_name = view_name
     356        self.var_name = var_name
    356357        self.args = args
    357358        self.kwargs = kwargs
    358359
    class URLNode(Node):  
    362363        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
    363364                       for k, v in self.kwargs.items()])
    364365        try:
    365             return reverse(self.view_name, args=args, kwargs=kwargs)
     366            r = reverse(self.view_name, args=args, kwargs=kwargs)
    366367        except NoReverseMatch:
    367368            project_name = settings.SETTINGS_MODULE.split('.')[0]
    368             return reverse(project_name + '.' + self.view_name,
     369            r = reverse(project_name + '.' + self.view_name,
    369370                           args=args, kwargs=kwargs)
     371        if self.var_name:
     372            context[self.var_name] = r
     373            return ''
     374        else:
     375            return r
    370376
    371377class WidthRatioNode(Node):
    372378    def __init__(self, val_expr, max_expr, max_width):
    def templatetag(parser, token):  
    10101016    return TemplateTagNode(tag)
    10111017templatetag = register.tag(templatetag)
    10121018
     1019#@register.url
    10131020def url(parser, token):
    10141021    """
    10151022    Returns an absolute URL matching given view with its parameters.
    def url(parser, token):  
    10401047        {% url app_name.client client.id %}
    10411048
    10421049    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.
    10431058    """
    1044     bits = token.contents.split(' ', 2)
     1059    bits = token.contents.split(' ')
    10451060    if len(bits) < 2:
    10461061        raise TemplateSyntaxError("'%s' takes at least one argument"
    10471062                                  " (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
    10481069    args = []
    10491070    kwargs = {}
    10501071    if len(bits) > 2:
    def url(parser, token):  
    10551076                kwargs[k] = parser.compile_filter(v)
    10561077            else:
    10571078                args.append(parser.compile_filter(arg))
    1058     return URLNode(bits[1], args, kwargs)
     1079    return URLNode(bits[1],var_name, args, kwargs)
    10591080url = register.tag(url)
    10601081
    10611082#@register.tag
Back to Top