Ticket #2606: 2606.2.diff

File 2606.2.diff, 2.8 KB (added by Ivan Sagalaev <Maniac@…>, 18 years ago)

Support for kwargs

  • django/template/defaulttags.py

     
    297297    def render(self, context):
    298298        return self.mapping.get(self.tagtype, '')
    299299
     300class URLNode(Node):
     301    def __init__(self, view_name, args, kwargs):
     302        self.view_name = view_name
     303        self.args = args
     304        self.kwargs = kwargs
     305     
     306    def render(self, context):
     307        from django.core.urlresolvers import reverse, NoReverseMatch
     308        args = [arg.resolve(context) for arg in self.args]
     309        kwargs = dict([(k, v.resolve(context)) for k, v in self.kwargs.items()])
     310        project_name = settings.SETTINGS_MODULE.split('.')[0]
     311        try:
     312            return reverse(project_name + '.' + self.view_name, args=args, kwargs=kwargs)
     313        except NoReverseMatch:
     314            return ''
     315
    300316class WidthRatioNode(Node):
    301317    def __init__(self, val_expr, max_expr, max_width):
    302318        self.val_expr = val_expr
     
    837853    return TemplateTagNode(tag)
    838854templatetag = register.tag(templatetag)
    839855
     856def url(parser, token):
     857    """
     858    Returns an absolute URL matching given view with its parameters. This is a mean to
     859    define links that aren't tied to a particular url configuration:
     860   
     861        {% url app_name.views.some_view arg1,arg2,name1=value1 %}
     862   
     863    The first argument is a path to a view in the form ``app_name.view_name`` (without
     864    the project name). Other arguments are comma-separated values that will be filled in
     865    place of positional and keyword arguments in the URL. All arguments for the URL should
     866    be present.
     867   
     868    For example if you have a view ``app_name.client`` taking client's id and the
     869    corresponding line in the urlconf looks like this:
     870   
     871        ('^client/(\d+)/$', 'app_name.client')
     872   
     873    ... and this app's urlconf is included into the project's urlconf under some path:
     874   
     875        ('^clients/', include('project_name.app_name.urls'))
     876   
     877    ... then in a template you can create a link for a certain client like this:
     878   
     879        {% url app_name.client client.id %}
     880   
     881    The URL will look like ``/clients/client/123/``.
     882    """
     883    bits = token.contents.split(' ', 2)
     884    if len(bits) < 2:
     885        raise TemplateSyntaxError, "'url' takes at least one argument (path to a view)"
     886    args = []
     887    kwargs = {}
     888    if len(bits) > 2:
     889        for arg in bits[2].split(','):
     890            if '=' in arg:
     891                k, v = arg.split('=', 1)
     892                kwargs[k] = parser.compile_filter(v)
     893            else:
     894                args.append(parser.compile_filter(arg))
     895    return URLNode(bits[1], args, kwargs)
     896url = register.tag(url)
     897
    840898#@register.tag
    841899def widthratio(parser, token):
    842900    """
Back to Top