Removed dirs parameter in template engine methods and related funtions.
Per deprecation timeline.
This commit is contained in:
parent
5e450c52aa
commit
b3641512c8
|
@ -12,7 +12,7 @@ from django.http import (
|
|||
)
|
||||
from django.template import RequestContext, loader
|
||||
from django.template.engine import (
|
||||
_context_instance_undefined, _dictionary_undefined, _dirs_undefined,
|
||||
_context_instance_undefined, _dictionary_undefined
|
||||
)
|
||||
from django.utils import six
|
||||
from django.utils.encoding import force_text
|
||||
|
@ -21,14 +21,13 @@ from django.utils.functional import Promise
|
|||
|
||||
def render_to_response(template_name, context=None,
|
||||
context_instance=_context_instance_undefined,
|
||||
content_type=None, status=None, dirs=_dirs_undefined,
|
||||
content_type=None, status=None,
|
||||
dictionary=_dictionary_undefined, using=None):
|
||||
"""
|
||||
Returns a HttpResponse whose content is filled with the result of calling
|
||||
django.template.loader.render_to_string() with the passed arguments.
|
||||
"""
|
||||
if (context_instance is _context_instance_undefined
|
||||
and dirs is _dirs_undefined
|
||||
and dictionary is _dictionary_undefined):
|
||||
# No deprecated arguments were passed - use the new code path
|
||||
content = loader.render_to_string(template_name, context, using=using)
|
||||
|
@ -36,7 +35,7 @@ def render_to_response(template_name, context=None,
|
|||
else:
|
||||
# Some deprecated arguments were passed - use the legacy code path
|
||||
content = loader.render_to_string(
|
||||
template_name, context, context_instance, dirs, dictionary,
|
||||
template_name, context, context_instance, dictionary,
|
||||
using=using)
|
||||
|
||||
return HttpResponse(content, content_type, status)
|
||||
|
@ -45,7 +44,7 @@ def render_to_response(template_name, context=None,
|
|||
def render(request, template_name, context=None,
|
||||
context_instance=_context_instance_undefined,
|
||||
content_type=None, status=None,
|
||||
dirs=_dirs_undefined, dictionary=_dictionary_undefined,
|
||||
dictionary=_dictionary_undefined,
|
||||
using=None):
|
||||
"""
|
||||
Returns a HttpResponse whose content is filled with the result of calling
|
||||
|
@ -53,7 +52,6 @@ def render(request, template_name, context=None,
|
|||
Uses a RequestContext by default.
|
||||
"""
|
||||
if (context_instance is _context_instance_undefined
|
||||
and dirs is _dirs_undefined
|
||||
and dictionary is _dictionary_undefined):
|
||||
# No deprecated arguments were passed - use the new code path
|
||||
# In Django 1.10, request should become a positional argument.
|
||||
|
@ -66,7 +64,7 @@ def render(request, template_name, context=None,
|
|||
else:
|
||||
context_instance = RequestContext(request)
|
||||
content = loader.render_to_string(
|
||||
template_name, context, context_instance, dirs, dictionary,
|
||||
template_name, context, context_instance, dictionary,
|
||||
using=using)
|
||||
|
||||
return HttpResponse(content, content_type, status)
|
||||
|
|
|
@ -10,7 +10,7 @@ from django.apps import apps
|
|||
from django.conf import settings
|
||||
from django.template import TemplateDoesNotExist
|
||||
from django.template.context import Context, RequestContext, make_context
|
||||
from django.template.engine import Engine, _dirs_undefined
|
||||
from django.template.engine import Engine
|
||||
from django.template.library import InvalidTemplateLibrary
|
||||
from django.utils import six
|
||||
from django.utils.deprecation import RemovedInDjango110Warning
|
||||
|
@ -35,9 +35,9 @@ class DjangoTemplates(BaseEngine):
|
|||
def from_string(self, template_code):
|
||||
return Template(self.engine.from_string(template_code), self)
|
||||
|
||||
def get_template(self, template_name, dirs=_dirs_undefined):
|
||||
def get_template(self, template_name):
|
||||
try:
|
||||
return Template(self.engine.get_template(template_name, dirs), self)
|
||||
return Template(self.engine.get_template(template_name), self)
|
||||
except TemplateDoesNotExist as exc:
|
||||
reraise(exc, self)
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ from .library import import_library
|
|||
|
||||
_context_instance_undefined = object()
|
||||
_dictionary_undefined = object()
|
||||
_dirs_undefined = object()
|
||||
|
||||
|
||||
class Engine(object):
|
||||
|
@ -167,19 +166,12 @@ class Engine(object):
|
|||
"""
|
||||
return Template(template_code, engine=self)
|
||||
|
||||
def get_template(self, template_name, dirs=_dirs_undefined):
|
||||
def get_template(self, template_name):
|
||||
"""
|
||||
Returns a compiled Template object for the given template name,
|
||||
handling template inheritance recursively.
|
||||
"""
|
||||
if dirs is _dirs_undefined:
|
||||
dirs = None
|
||||
else:
|
||||
warnings.warn(
|
||||
"The dirs argument of get_template is deprecated.",
|
||||
RemovedInDjango110Warning, stacklevel=2)
|
||||
|
||||
template, origin = self.find_template(template_name, dirs)
|
||||
template, origin = self.find_template(template_name)
|
||||
if not hasattr(template, 'render'):
|
||||
# template needs to be compiled
|
||||
template = Template(template, origin, template_name, engine=self)
|
||||
|
@ -193,7 +185,6 @@ class Engine(object):
|
|||
|
||||
def render_to_string(self, template_name, context=None,
|
||||
context_instance=_context_instance_undefined,
|
||||
dirs=_dirs_undefined,
|
||||
dictionary=_dictionary_undefined):
|
||||
if context_instance is _context_instance_undefined:
|
||||
context_instance = None
|
||||
|
@ -201,14 +192,6 @@ class Engine(object):
|
|||
warnings.warn(
|
||||
"The context_instance argument of render_to_string is "
|
||||
"deprecated.", RemovedInDjango110Warning, stacklevel=2)
|
||||
if dirs is _dirs_undefined:
|
||||
# Do not set dirs to None here to avoid triggering the deprecation
|
||||
# warning in select_template or get_template.
|
||||
pass
|
||||
else:
|
||||
warnings.warn(
|
||||
"The dirs argument of render_to_string is deprecated.",
|
||||
RemovedInDjango110Warning, stacklevel=2)
|
||||
if dictionary is _dictionary_undefined:
|
||||
dictionary = None
|
||||
else:
|
||||
|
@ -218,9 +201,9 @@ class Engine(object):
|
|||
context = dictionary
|
||||
|
||||
if isinstance(template_name, (list, tuple)):
|
||||
t = self.select_template(template_name, dirs)
|
||||
t = self.select_template(template_name)
|
||||
else:
|
||||
t = self.get_template(template_name, dirs)
|
||||
t = self.get_template(template_name)
|
||||
if not context_instance:
|
||||
# Django < 1.8 accepted a Context in `context` even though that's
|
||||
# unintended. Preserve this ability but don't rewrap `context`.
|
||||
|
@ -235,25 +218,16 @@ class Engine(object):
|
|||
with context_instance.push(context):
|
||||
return t.render(context_instance)
|
||||
|
||||
def select_template(self, template_name_list, dirs=_dirs_undefined):
|
||||
def select_template(self, template_name_list):
|
||||
"""
|
||||
Given a list of template names, returns the first that can be loaded.
|
||||
"""
|
||||
if dirs is _dirs_undefined:
|
||||
# Do not set dirs to None here to avoid triggering the deprecation
|
||||
# warning in get_template.
|
||||
pass
|
||||
else:
|
||||
warnings.warn(
|
||||
"The dirs argument of select_template is deprecated.",
|
||||
RemovedInDjango110Warning, stacklevel=2)
|
||||
|
||||
if not template_name_list:
|
||||
raise TemplateDoesNotExist("No template names provided")
|
||||
not_found = []
|
||||
for template_name in template_name_list:
|
||||
try:
|
||||
return self.get_template(template_name, dirs)
|
||||
return self.get_template(template_name)
|
||||
except TemplateDoesNotExist as exc:
|
||||
if exc.args[0] not in not_found:
|
||||
not_found.append(exc.args[0])
|
||||
|
|
|
@ -4,14 +4,12 @@ from django.utils.deprecation import RemovedInDjango110Warning
|
|||
|
||||
from . import engines
|
||||
from .backends.django import DjangoTemplates
|
||||
from .engine import (
|
||||
_context_instance_undefined, _dictionary_undefined, _dirs_undefined,
|
||||
)
|
||||
from .engine import _context_instance_undefined, _dictionary_undefined
|
||||
from .exceptions import TemplateDoesNotExist
|
||||
from .loaders import base
|
||||
|
||||
|
||||
def get_template(template_name, dirs=_dirs_undefined, using=None):
|
||||
def get_template(template_name, using=None):
|
||||
"""
|
||||
Loads and returns a template for the given name.
|
||||
|
||||
|
@ -21,16 +19,6 @@ def get_template(template_name, dirs=_dirs_undefined, using=None):
|
|||
engines = _engine_list(using)
|
||||
for engine in engines:
|
||||
try:
|
||||
# This is required for deprecating the dirs argument. Simply
|
||||
# return engine.get_template(template_name) in Django 1.10.
|
||||
if isinstance(engine, DjangoTemplates):
|
||||
return engine.get_template(template_name, dirs)
|
||||
elif dirs is not _dirs_undefined:
|
||||
warnings.warn(
|
||||
"Skipping template backend %s because its get_template "
|
||||
"method doesn't support the dirs argument." % engine.name,
|
||||
stacklevel=2)
|
||||
else:
|
||||
return engine.get_template(template_name)
|
||||
except TemplateDoesNotExist as e:
|
||||
chain.append(e)
|
||||
|
@ -38,7 +26,7 @@ def get_template(template_name, dirs=_dirs_undefined, using=None):
|
|||
raise TemplateDoesNotExist(template_name, chain=chain)
|
||||
|
||||
|
||||
def select_template(template_name_list, dirs=_dirs_undefined, using=None):
|
||||
def select_template(template_name_list, using=None):
|
||||
"""
|
||||
Loads and returns a template for one of the given names.
|
||||
|
||||
|
@ -51,16 +39,6 @@ def select_template(template_name_list, dirs=_dirs_undefined, using=None):
|
|||
for template_name in template_name_list:
|
||||
for engine in engines:
|
||||
try:
|
||||
# This is required for deprecating the dirs argument. Simply
|
||||
# use engine.get_template(template_name) in Django 1.10.
|
||||
if isinstance(engine, DjangoTemplates):
|
||||
return engine.get_template(template_name, dirs)
|
||||
elif dirs is not _dirs_undefined:
|
||||
warnings.warn(
|
||||
"Skipping template backend %s because its get_template "
|
||||
"method doesn't support the dirs argument." % engine.name,
|
||||
stacklevel=2)
|
||||
else:
|
||||
return engine.get_template(template_name)
|
||||
except TemplateDoesNotExist as e:
|
||||
chain.append(e)
|
||||
|
@ -73,7 +51,6 @@ def select_template(template_name_list, dirs=_dirs_undefined, using=None):
|
|||
|
||||
def render_to_string(template_name, context=None,
|
||||
context_instance=_context_instance_undefined,
|
||||
dirs=_dirs_undefined,
|
||||
dictionary=_dictionary_undefined,
|
||||
request=None, using=None):
|
||||
"""
|
||||
|
@ -82,7 +59,6 @@ def render_to_string(template_name, context=None,
|
|||
template_name may be a string or a list of strings.
|
||||
"""
|
||||
if (context_instance is _context_instance_undefined
|
||||
and dirs is _dirs_undefined
|
||||
and dictionary is _dictionary_undefined):
|
||||
# No deprecated arguments were passed - use the new code path
|
||||
if isinstance(template_name, (list, tuple)):
|
||||
|
@ -106,17 +82,12 @@ def render_to_string(template_name, context=None,
|
|||
"when some deprecated arguments are passed.")
|
||||
# Hack -- use the internal Engine instance of DjangoTemplates.
|
||||
return engine.engine.render_to_string(
|
||||
template_name, context, context_instance, dirs, dictionary)
|
||||
template_name, context, context_instance, dictionary)
|
||||
elif context_instance is not _context_instance_undefined:
|
||||
warnings.warn(
|
||||
"Skipping template backend %s because its render_to_string "
|
||||
"method doesn't support the context_instance argument." %
|
||||
engine.name, stacklevel=2)
|
||||
elif dirs is not _dirs_undefined:
|
||||
warnings.warn(
|
||||
"Skipping template backend %s because its render_to_string "
|
||||
"method doesn't support the dirs argument." % engine.name,
|
||||
stacklevel=2)
|
||||
elif dictionary is not _dictionary_undefined:
|
||||
warnings.warn(
|
||||
"Skipping template backend %s because its render_to_string "
|
||||
|
|
|
@ -15,7 +15,7 @@ introduce controlled coupling for convenience's sake.
|
|||
``render``
|
||||
==========
|
||||
|
||||
.. function:: render(request, template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, dirs=_dirs_undefined, using=None)
|
||||
.. function:: render(request, template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, using=None)
|
||||
|
||||
Combines a given template with a given context dictionary and returns an
|
||||
:class:`~django.http.HttpResponse` object with that rendered text.
|
||||
|
@ -75,10 +75,6 @@ Optional arguments
|
|||
|
||||
The ``using`` parameter was added.
|
||||
|
||||
.. deprecated:: 1.8
|
||||
|
||||
The ``dirs`` parameter was deprecated.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
|
@ -107,7 +103,7 @@ This example is equivalent to::
|
|||
``render_to_response``
|
||||
======================
|
||||
|
||||
.. function:: render_to_response(template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, dirs=_dirs_undefined, using=None)
|
||||
.. function:: render_to_response(template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, using=None)
|
||||
|
||||
Renders a given template with a given context dictionary and returns an
|
||||
:class:`~django.http.HttpResponse` object with that rendered text.
|
||||
|
@ -165,10 +161,6 @@ Optional arguments
|
|||
|
||||
The ``status`` and ``using`` parameters were added.
|
||||
|
||||
.. deprecated:: 1.8
|
||||
|
||||
The ``dirs`` parameter was deprecated.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ Usage
|
|||
|
||||
The ``django.template.loader`` module defines two functions to load templates.
|
||||
|
||||
.. function:: get_template(template_name, dirs=_dirs_undefined, using=None)
|
||||
.. function:: get_template(template_name, using=None)
|
||||
|
||||
This function loads the template with the given name and returns a
|
||||
``Template`` object.
|
||||
|
@ -115,10 +115,6 @@ The ``django.template.loader`` module defines two functions to load templates.
|
|||
If you want to restrict the search to a particular template engine, pass
|
||||
the engine's :setting:`NAME <TEMPLATES-NAME>` in the ``using`` argument.
|
||||
|
||||
.. deprecated:: 1.8
|
||||
|
||||
The ``dirs`` parameter was deprecated.
|
||||
|
||||
.. versionchanged:: 1.8
|
||||
|
||||
The ``using`` parameter was added.
|
||||
|
@ -128,16 +124,12 @@ The ``django.template.loader`` module defines two functions to load templates.
|
|||
``get_template()`` returns a backend-dependent ``Template`` instead
|
||||
of a :class:`django.template.Template`.
|
||||
|
||||
.. function:: select_template(template_name_list, dirs=_dirs_undefined, using=None)
|
||||
.. function:: select_template(template_name_list, using=None)
|
||||
|
||||
``select_template()`` is just like ``get_template()``, except it takes a
|
||||
list of template names. It tries each name in order and returns the first
|
||||
template that exists.
|
||||
|
||||
.. deprecated:: 1.8
|
||||
|
||||
The ``dirs`` parameter was deprecated.
|
||||
|
||||
.. versionchanged:: 1.8
|
||||
|
||||
The ``using`` parameter was added.
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
spam eggs
|
|
@ -32,13 +32,6 @@ class ShortcutTests(SimpleTestCase):
|
|||
self.assertEqual(response.content, b'FOO.BAR..\n')
|
||||
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
||||
def test_render_to_response_with_dirs(self):
|
||||
response = self.client.get('/render_to_response/dirs/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, b'spam eggs\n')
|
||||
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
||||
|
||||
def test_render_to_response_with_status(self):
|
||||
response = self.client.get('/render_to_response/status/')
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
@ -101,10 +94,3 @@ class ShortcutTests(SimpleTestCase):
|
|||
self.assertEqual(response.content, b'DTL\n')
|
||||
response = self.client.get('/render/using/?using=jinja2')
|
||||
self.assertEqual(response.content, b'Jinja2\n')
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
||||
def test_render_with_dirs(self):
|
||||
response = self.client.get('/render/dirs/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, b'spam eggs\n')
|
||||
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
||||
|
|
|
@ -7,7 +7,6 @@ urlpatterns = [
|
|||
url(r'^render_to_response/multiple_templates/$', views.render_to_response_view_with_multiple_templates),
|
||||
url(r'^render_to_response/request_context/$', views.render_to_response_view_with_request_context),
|
||||
url(r'^render_to_response/content_type/$', views.render_to_response_view_with_content_type),
|
||||
url(r'^render_to_response/dirs/$', views.render_to_response_view_with_dirs),
|
||||
url(r'^render_to_response/status/$', views.render_to_response_view_with_status),
|
||||
url(r'^render_to_response/using/$', views.render_to_response_view_with_using),
|
||||
url(r'^render_to_response/context_instance_misuse/$', views.render_to_response_with_context_instance_misuse),
|
||||
|
@ -15,7 +14,6 @@ urlpatterns = [
|
|||
url(r'^render/multiple_templates/$', views.render_view_with_multiple_templates),
|
||||
url(r'^render/base_context/$', views.render_view_with_base_context),
|
||||
url(r'^render/content_type/$', views.render_view_with_content_type),
|
||||
url(r'^render/dirs/$', views.render_with_dirs),
|
||||
url(r'^render/status/$', views.render_view_with_status),
|
||||
url(r'^render/using/$', views.render_view_with_using),
|
||||
]
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
import os.path
|
||||
|
||||
from django.shortcuts import render, render_to_response
|
||||
from django.template import Context, RequestContext
|
||||
from django.utils._os import upath
|
||||
|
||||
dirs = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
|
||||
|
||||
|
||||
def render_to_response_view(request):
|
||||
|
@ -38,10 +33,6 @@ def render_to_response_view_with_content_type(request):
|
|||
}, content_type='application/x-rendertest')
|
||||
|
||||
|
||||
def render_to_response_view_with_dirs(request):
|
||||
return render_to_response('render_dirs_test.html', dirs=dirs)
|
||||
|
||||
|
||||
def render_to_response_view_with_status(request):
|
||||
return render_to_response('shortcuts/render_test.html', {
|
||||
'foo': 'FOO',
|
||||
|
@ -95,10 +86,6 @@ def render_view_with_content_type(request):
|
|||
}, content_type='application/x-rendertest')
|
||||
|
||||
|
||||
def render_with_dirs(request):
|
||||
return render(request, 'render_dirs_test.html', dirs=dirs)
|
||||
|
||||
|
||||
def render_view_with_status(request):
|
||||
return render(request, 'shortcuts/render_test.html', {
|
||||
'foo': 'FOO',
|
||||
|
|
|
@ -89,28 +89,3 @@ class LoaderTests(SimpleTestCase):
|
|||
|
||||
template = engine.get_template('priority/foo.html')
|
||||
self.assertEqual(template.render(Context()), 'priority\n')
|
||||
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
||||
class TemplateDirsOverrideTests(SimpleTestCase):
|
||||
DIRS = ((OTHER_DIR, ), [OTHER_DIR])
|
||||
|
||||
def setUp(self):
|
||||
self.engine = Engine()
|
||||
|
||||
def test_render_to_string(self):
|
||||
for dirs in self.DIRS:
|
||||
self.assertEqual(
|
||||
self.engine.render_to_string('test_dirs.html', dirs=dirs),
|
||||
'spam eggs\n',
|
||||
)
|
||||
|
||||
def test_get_template(self):
|
||||
for dirs in self.DIRS:
|
||||
template = self.engine.get_template('test_dirs.html', dirs=dirs)
|
||||
self.assertEqual(template.render(Context()), 'spam eggs\n')
|
||||
|
||||
def test_select_template(self):
|
||||
for dirs in self.DIRS:
|
||||
template = self.engine.select_template(['test_dirs.html'], dirs=dirs)
|
||||
self.assertEqual(template.render(Context()), 'spam eggs\n')
|
||||
|
|
Loading…
Reference in New Issue