Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21529 closed Cleanup/optimization (fixed)

url tag urlencodes arguments irreversibly

Reported by: leo@… Owned by: nobody
Component: Documentation Version: 1.6
Severity: Normal Keywords: url tag urlencode javascript js
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Somewhere between 1.4 and 1.6 the url tag started urlencoding arguments before outputing the URL. This breaks code that wants to build the url inside javascript in the following way:

<script>
  var url = '{% url "some_view" "'+some_js_argument+'" %}'
</script>

On 1.4 this generates '/some/view/'+some_js_argument+'/'
On 1.6 this generates '/some/view/%27%2Bsome_js_argument%2B%27'

While I understand the rationale behind urlencoding per default, it would be nice to provide a way to avoid it. "|safe" seems to have no effect and even a custom filter to urldecode the output doesn't help, since urlencoding seems to be the last step before output.
Please consider providing a way to disable this, either by filter or by options to the url tag, so that we're not forced to use kludgy workarounds like

<script>
  var url = '{% url "some_view" "ARGUMENT_BY_JS" %}'.replace(/ARGUMENT_BY_JS/, some_js_argument)
</script>

Additionally, this might be a good thing to mention in the docs.

This is admittedly somewhere between a bug and a feature request, but since this breaks old code, I've decided to submit it as a regression bug.

Attachments (1)

21529.diff (758 bytes ) - added by Tim Graham 11 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by Florian Apolloner, 11 years ago

Component: Template systemDocumentation
Needs documentation: set
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I think the url tag should always return proper URLs, but we should document it clearly.

by Tim Graham, 11 years ago

Attachment: 21529.diff added

comment:2 by Tim Graham, 11 years ago

Has patch: set
Needs documentation: unset

comment:3 by Tim Graham, 11 years ago

For the record, this was introduced in Django 1.6.

comment:4 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In dfc092622e5e55081f9a76fddea752494c4505ba:

Fixed #21529 -- Noted that {% url %} encodes its output (refs #13260).

comment:5 by Tim Graham <timograham@…>, 11 years ago

In a4c32d70c2bcf9731b6d6ff3370d2260ab4812af:

[1.6.x] Fixed #21529 -- Noted that {% url %} encodes its output (refs #13260).

Backport of dfc092622e from master

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