Opened 3 years ago

Closed 3 years ago

Last modified 3 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


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:

  var url = '{% url "some_view" "'+some_js_argument+'" %}'

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

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

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 3 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 3 years ago by Florian Apolloner

Component: Template systemDocumentation
Needs documentation: set
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

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

Changed 3 years ago by Tim Graham

Attachment: 21529.diff added

comment:2 Changed 3 years ago by Tim Graham

Has patch: set
Needs documentation: unset

comment:3 Changed 3 years ago by Tim Graham

For the record, this was introduced in Django 1.6.

comment:4 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In dfc092622e5e55081f9a76fddea752494c4505ba:

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

comment:5 Changed 3 years ago by Tim Graham <timograham@…>

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