Opened 17 months ago

Closed 15 months ago

Last modified 15 months 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 timo 15 months ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 17 months ago by apollo13

  • Component changed from Template system to Documentation
  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to Cleanup/optimization

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

Changed 15 months ago by timo

comment:2 Changed 15 months ago by timo

  • Has patch set
  • Needs documentation unset

comment:3 Changed 15 months ago by timo

For the record, this was introduced in Django 1.6.

comment:4 Changed 15 months ago by Tim Graham <timograham@…>

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

In dfc092622e5e55081f9a76fddea752494c4505ba:

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

comment:5 Changed 15 months 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