Opened 17 years ago
Closed 17 years ago
#7049 closed (wontfix)
Tag url first argument path
Reported by: | 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)
Change History (3)
by , 17 years ago
Attachment: | defaulttags.diff added |
---|
comment:1 by , 17 years ago
Has patch: | set |
---|
comment:2 by , 17 years ago
Resolution: | → wontfix |
---|---|
Status: | new → 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.
path for url tag