Opened 8 years ago

Closed 8 years ago

#7049 closed (wontfix)

Tag url first argument path

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


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:

---      (revision 7438)
+++      (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()])
-            return reverse(self.view_name, args=args, kwargs=kwargs)
+            return reverse(view_name, args=args, kwargs=kwargs)
         except NoReverseMatch:
                 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)
-    return URLNode(bits[1], args, kwargs)
+    return URLNode(parser.compile_filter(bits[1]), args, kwargs)
 url = register.tag(url)

Attachments (1)

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

Download all attachments as: .zip

Change History (3)

Changed 8 years ago by tonal@…

path for url tag

comment:1 Changed 8 years ago by Alex

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 8 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

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