From 1a04b1762b50ea4d09eb1dc192d57172750b80aa Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 19 Dec 2016 13:44:02 -0500 Subject: [PATCH] Refs #25484 -- Made non-staticfiles {% static %} tag quote its output. --- django/templatetags/static.py | 4 ++-- tests/template_tests/syntax_tests/test_static.py | 12 +++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/django/templatetags/static.py b/django/templatetags/static.py index 0f46fa33daa..e4789bda71b 100644 --- a/django/templatetags/static.py +++ b/django/templatetags/static.py @@ -2,7 +2,7 @@ from django import template from django.apps import apps from django.utils.encoding import iri_to_uri from django.utils.html import conditional_escape -from django.utils.six.moves.urllib.parse import urljoin +from django.utils.six.moves.urllib.parse import quote, urljoin register = template.Library() @@ -116,7 +116,7 @@ class StaticNode(template.Node): from django.contrib.staticfiles.storage import staticfiles_storage return staticfiles_storage.url(path) else: - return urljoin(PrefixNode.handle_simple("STATIC_URL"), path) + return urljoin(PrefixNode.handle_simple("STATIC_URL"), quote(path)) @classmethod def handle_token(cls, parser, token): diff --git a/tests/template_tests/syntax_tests/test_static.py b/tests/template_tests/syntax_tests/test_static.py index d5ca9ecf609..deb0ce6c783 100644 --- a/tests/template_tests/syntax_tests/test_static.py +++ b/tests/template_tests/syntax_tests/test_static.py @@ -51,13 +51,7 @@ class StaticTagTests(SimpleTestCase): output = self.engine.render_to_string('static-statictag04', {'base_css': 'admin/base.css'}) self.assertEqual(output, urljoin(settings.STATIC_URL, 'admin/base.css')) - @setup({'static-statictag05': '{% load static %}{% static "test.html?foo=1&bar=2" %}'}) - def test_static_escapes_urls(self): + @setup({'static-statictag05': '{% load static %}{% static "special?chars"ed.html" %}'}) + def test_static_quotes_urls(self): output = self.engine.render_to_string('static-statictag05') - self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/test.html?foo=1&bar=2')) - - @setup({'static-statictag06': '{% load static %}' - '{% autoescape off %}{% static "test.html?foo=1&bar=2" %}{% endautoescape %}'}) - def test_static_escapes_urls_autoescape_off(self): - output = self.engine.render_to_string('static-statictag06') - self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/test.html?foo=1&bar=2')) + self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/special%3Fchars%26quoted.html'))