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.
This commit is contained in:
parent
118592663d
commit
eaa1a22341
|
@ -80,7 +80,7 @@ def render_to_string(template_name, context=None,
|
||||||
context_instance=_context_instance_undefined,
|
context_instance=_context_instance_undefined,
|
||||||
dirs=_dirs_undefined,
|
dirs=_dirs_undefined,
|
||||||
dictionary=_dictionary_undefined,
|
dictionary=_dictionary_undefined,
|
||||||
using=None):
|
request=None, using=None):
|
||||||
"""
|
"""
|
||||||
Loads a template and renders it with a context. Returns a string.
|
Loads a template and renders it with a context. Returns a string.
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ def render_to_string(template_name, context=None,
|
||||||
template = select_template(template_name, using=using)
|
template = select_template(template_name, using=using)
|
||||||
else:
|
else:
|
||||||
template = get_template(template_name, using=using)
|
template = get_template(template_name, using=using)
|
||||||
return template.render(context)
|
return template.render(context, request)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Some deprecated arguments were passed - use the legacy code path
|
# Some deprecated arguments were passed - use the legacy code path
|
||||||
|
@ -104,6 +104,11 @@ def render_to_string(template_name, context=None,
|
||||||
# to Django templates. Remove Engine.render_to_string() at the
|
# to Django templates. Remove Engine.render_to_string() at the
|
||||||
# same time as this code path in Django 2.0.
|
# same time as this code path in Django 2.0.
|
||||||
if isinstance(engine, DjangoTemplates):
|
if isinstance(engine, DjangoTemplates):
|
||||||
|
if request is not None:
|
||||||
|
raise ValueError(
|
||||||
|
"render_to_string doesn't support the request argument "
|
||||||
|
"when some deprecated arguments are passed.")
|
||||||
|
continue
|
||||||
# Hack -- use the internal Engine instance of DjangoTemplates.
|
# Hack -- use the internal Engine instance of DjangoTemplates.
|
||||||
return engine.engine.render_to_string(
|
return engine.engine.render_to_string(
|
||||||
template_name, context, context_instance, dirs, dictionary)
|
template_name, context, context_instance, dirs, dictionary)
|
||||||
|
|
|
@ -1351,8 +1351,10 @@ The following functions will no longer accept the ``dictionary`` and
|
||||||
Use the ``context`` parameter instead. When ``dictionary`` is passed as a
|
Use the ``context`` parameter instead. When ``dictionary`` is passed as a
|
||||||
positional argument, which is the most common idiom, no changes are needed.
|
positional argument, which is the most common idiom, no changes are needed.
|
||||||
|
|
||||||
There is no replacement for ``context_instance``. All data must be passed to
|
If you're passing a :class:`~django.template.Context` in ``context_instance``,
|
||||||
templates through the ``context`` dict.
|
pass a :class:`dict` in the ``context`` parameter instead. If you're passing a
|
||||||
|
:class:`~django.template.RequestContext`, pass the request separately in the
|
||||||
|
``request`` parameter.
|
||||||
|
|
||||||
``dirs`` argument of template-finding functions
|
``dirs`` argument of template-finding functions
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{{ request.path }}
|
|
@ -1,4 +1,5 @@
|
||||||
from django.test import override_settings, SimpleTestCase
|
from django.test import override_settings, SimpleTestCase
|
||||||
|
from django.test.client import RequestFactory
|
||||||
from django.template import TemplateDoesNotExist
|
from django.template import TemplateDoesNotExist
|
||||||
from django.template.loader import (
|
from django.template.loader import (
|
||||||
get_template, select_template, render_to_string)
|
get_template, select_template, render_to_string)
|
||||||
|
@ -10,6 +11,11 @@ from django.template.loader import (
|
||||||
}, {
|
}, {
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
'django.template.context_processors.request',
|
||||||
|
],
|
||||||
|
},
|
||||||
}])
|
}])
|
||||||
class TemplateLoaderTests(SimpleTestCase):
|
class TemplateLoaderTests(SimpleTestCase):
|
||||||
|
|
||||||
|
@ -66,6 +72,11 @@ class TemplateLoaderTests(SimpleTestCase):
|
||||||
content = render_to_string("template_loader/goodbye.html")
|
content = render_to_string("template_loader/goodbye.html")
|
||||||
self.assertEqual(content, "Goodbye! (Django templates)\n")
|
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):
|
def test_render_to_string_using_engine(self):
|
||||||
content = render_to_string("template_loader/hello.html", using="django")
|
content = render_to_string("template_loader/hello.html", using="django")
|
||||||
self.assertEqual(content, "Hello! (Django templates)\n")
|
self.assertEqual(content, "Hello! (Django templates)\n")
|
||||||
|
|
Loading…
Reference in New Issue