From eaa1a22341aef5b92f5c3cd682f01e61c4159262 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 3 Jan 2015 19:06:36 +0100 Subject: Added a request argument to render_to_string. This is for consistency with Template.render. It adds a little bit of knowledge about HTTP requests in django.template.loader but I think consistency trumps purity. --- tests/template_loader/templates/template_loader/request.html | 1 + tests/template_loader/tests.py | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/template_loader/templates/template_loader/request.html (limited to 'tests') diff --git a/tests/template_loader/templates/template_loader/request.html b/tests/template_loader/templates/template_loader/request.html new file mode 100644 index 0000000000..5d899f6000 --- /dev/null +++ b/tests/template_loader/templates/template_loader/request.html @@ -0,0 +1 @@ +{{ request.path }} diff --git a/tests/template_loader/tests.py b/tests/template_loader/tests.py index 3ec785a6a4..12bfa1b543 100644 --- a/tests/template_loader/tests.py +++ b/tests/template_loader/tests.py @@ -1,4 +1,5 @@ from django.test import override_settings, SimpleTestCase +from django.test.client import RequestFactory from django.template import TemplateDoesNotExist from django.template.loader import ( get_template, select_template, render_to_string) @@ -10,6 +11,11 @@ from django.template.loader import ( }, { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + ], + }, }]) class TemplateLoaderTests(SimpleTestCase): @@ -66,6 +72,11 @@ class TemplateLoaderTests(SimpleTestCase): content = render_to_string("template_loader/goodbye.html") self.assertEqual(content, "Goodbye! (Django templates)\n") + def test_render_to_string_with_request(self): + request = RequestFactory().get('/foobar/') + content = render_to_string("template_loader/request.html", request=request) + self.assertEqual(content, "/foobar/\n") + def test_render_to_string_using_engine(self): content = render_to_string("template_loader/hello.html", using="django") self.assertEqual(content, "Hello! (Django templates)\n") -- cgit v1.3