Opened 17 years ago

Closed 17 years ago

#7049 closed (wontfix)

Tag url first argument path

Reported by: tonal@… Owned by: nobody
Component: Template system Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Tag {%url%} expects string in the first parameter.
What prevents its use in a cycle thus:

{% for key, value in titles %}
  <a href="{% url key %}" class="a_white">{{ value }}</a>
{% endfor %}

The submitted patch solves this problem:

Index: defaulttags.py
===================================================================
--- defaulttags.py      (revision 7438)
+++ defaulttags.py      (working copy)
@@ -357,14 +357,17 @@
     def render(self, context):
         from django.core.urlresolvers import reverse, NoReverseMatch
         args = [arg.resolve(context) for arg in self.args]
+        view_name = self.view_name.resolve(context)
+        if not view_name:
+            view_name = self.view_name.token
         kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
                        for k, v in self.kwargs.items()])
         try:
-            return reverse(self.view_name, args=args, kwargs=kwargs)
+            return reverse(view_name, args=args, kwargs=kwargs)
         except NoReverseMatch:
             try:
                 project_name = settings.SETTINGS_MODULE.split('.')[0]
-                return reverse(project_name + '.' + self.view_name,
+                return reverse(project_name + '.' + view_name,
                                args=args, kwargs=kwargs)
             except NoReverseMatch:
                 return ''
@@ -1050,7 +1053,7 @@
                 kwargs[k] = parser.compile_filter(v)
             else:
                 args.append(parser.compile_filter(arg))
-    return URLNode(bits[1], args, kwargs)
+    return URLNode(parser.compile_filter(bits[1]), args, kwargs)
 url = register.tag(url)
 
 #@register.tag

Attachments (1)

defaulttags.diff (1.4 KB ) - added by tonal@… 17 years ago.
path for url tag

Download all attachments as: .zip

Change History (3)

by tonal@…, 17 years ago

Attachment: defaulttags.diff added

path for url tag

comment:1 by Alex Gaynor, 17 years ago

Has patch: set

comment:2 by Malcolm Tredinnick, 17 years ago

Resolution: wontfix
Status: newclosed

There was some discussion about allowing the use of a variable as the first argument to the url tag some months back and it didn't get any real support.

Your particular approach is flawed, in case you're intending to use it locally, since there's no way for the template writer to guarantee that their variable name will never conflict with the name of a url pattern (from some other app, for example). So it would need a specific way to differentiate between a string and a variable there.

Note: See TracTickets for help on using tickets.
Back to Top