Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix template.render deprecation warnings for Django 1.9 #3654

Merged
merged 1 commit into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import django
from django.conf import settings
from django.db import connection, transaction
from django.template import Context, RequestContext, Template
from django.utils import six
from django.views.generic import View

Expand Down Expand Up @@ -191,6 +192,7 @@ def apply_markdown(text):
except ImportError:
DecimalValidator = None


def set_rollback():
if hasattr(transaction, 'set_rollback'):
if connection.settings_dict.get('ATOMIC_REQUESTS', False):
Expand All @@ -206,3 +208,25 @@ def set_rollback():
else:
# transaction not managed
pass


def template_render(template, context=None, request=None):
"""
Passing Context or RequestContext to Template.render is deprecated in 1.9+,
see https://github.com/django/django/pull/3883 and
https://github.com/django/django/blob/1.9rc1/django/template/backends/django.py#L82-L84

:param template: Template instance
:param context: dict
:param request: Request instance
:return: rendered template as SafeText instance
"""
if django.VERSION < (1, 9) or isinstance(template, Template):
if request:
context = RequestContext(request, context)
else:
context = Context(context)
return template.render(context)
# backends template, e.g. django.template.backends.django.Template
else:
return template.render(context, request=request)
20 changes: 10 additions & 10 deletions rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.template import Context, loader
from django.template import loader
from django.utils import six
from django.utils.translation import ugettext_lazy as _

from rest_framework.compat import (
crispy_forms, distinct, django_filters, guardian
crispy_forms, distinct, django_filters, guardian, template_render
)
from rest_framework.settings import api_settings

Expand Down Expand Up @@ -122,11 +122,11 @@ def to_html(self, request, queryset, view):
filter_instance = filter_class(request.query_params, queryset=queryset)
else:
filter_instance = None
context = Context({
context = {
'filter': filter_instance
})
}
template = loader.get_template(self.template)
return template.render(context)
return template_render(template, context)


class SearchFilter(BaseFilterBackend):
Expand Down Expand Up @@ -185,12 +185,12 @@ def to_html(self, request, queryset, view):

term = self.get_search_terms(request)
term = term[0] if term else ''
context = Context({
context = {
'param': self.search_param,
'term': term
})
}
template = loader.get_template(self.template)
return template.render(context)
return template_render(template, context)


class OrderingFilter(BaseFilterBackend):
Expand Down Expand Up @@ -284,8 +284,8 @@ def get_template_context(self, request, queryset, view):

def to_html(self, request, queryset, view):
template = loader.get_template(self.template)
context = Context(self.get_template_context(request, queryset, view))
return template.render(context)
context = self.get_template_context(request, queryset, view)
return template_render(template, context)


class DjangoObjectPermissionsFilter(BaseFilterBackend):
Expand Down
15 changes: 8 additions & 7 deletions rest_framework/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

from django.core.paginator import Paginator as DjangoPaginator
from django.core.paginator import InvalidPage
from django.template import Context, loader
from django.template import loader
from django.utils import six
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.translation import ugettext_lazy as _

from rest_framework.compat import template_render
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.settings import api_settings
Expand Down Expand Up @@ -273,8 +274,8 @@ def page_number_to_url(page_number):

def to_html(self):
template = loader.get_template(self.template)
context = Context(self.get_html_context())
return template.render(context)
context = self.get_html_context()
return template_render(template, context)


class LimitOffsetPagination(BasePagination):
Expand Down Expand Up @@ -389,8 +390,8 @@ def page_number_to_url(page_number):

def to_html(self):
template = loader.get_template(self.template)
context = Context(self.get_html_context())
return template.render(context)
context = self.get_html_context()
return template_render(template, context)


class CursorPagination(BasePagination):
Expand Down Expand Up @@ -692,5 +693,5 @@ def get_html_context(self):

def to_html(self):
template = loader.get_template(self.template)
context = Context(self.get_html_context())
return template.render(context)
context = self.get_html_context()
return template_render(template, context)
30 changes: 14 additions & 16 deletions rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.paginator import Page
from django.http.multipartparser import parse_header
from django.template import Context, RequestContext, Template, loader
from django.template import Template, loader
from django.test.client import encode_multipart
from django.utils import six

from rest_framework import VERSION, exceptions, serializers, status
from rest_framework.compat import (
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, template_render
)
from rest_framework.exceptions import ParseError
from rest_framework.request import is_form_media_type, override_method
Expand Down Expand Up @@ -168,15 +168,15 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
template = self.resolve_template(template_names)

context = self.resolve_context(data, request, response)
return template.render(context)
return template_render(template, context, request=request)

def resolve_template(self, template_names):
return loader.select_template(template_names)

def resolve_context(self, data, request, response):
if response.exception:
data['status_code'] = response.status_code
return RequestContext(request, data)
return data

def get_template_names(self, response, view):
if response.template_name:
Expand Down Expand Up @@ -230,7 +230,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
request = renderer_context['request']
template = self.get_exception_template(response)
context = self.resolve_context(data, request, response)
return template.render(context)
return template_render(template, context, request=request)

return data

Expand Down Expand Up @@ -333,8 +333,8 @@ def render_field(self, field, parent_style):
template_name = style['template_pack'].strip('/') + '/' + style['base_template']

template = loader.get_template(template_name)
context = Context({'field': field, 'style': style})
return template.render(context)
context = {'field': field, 'style': style}
return template_render(template, context)

def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Expand All @@ -350,11 +350,11 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
template_pack = style['template_pack'].strip('/')
template_name = template_pack + '/' + self.base_template
template = loader.get_template(template_name)
context = Context({
context = {
'form': form,
'style': style
})
return template.render(context)
}
return template_render(template, context)


class BrowsableAPIRenderer(BaseRenderer):
Expand Down Expand Up @@ -600,8 +600,8 @@ def get_filter_form(self, data, view, request):
return

template = loader.get_template(self.filter_template)
context = Context({'elements': elements})
return template.render(context)
context = {'elements': elements}
return template_render(template, context)

def get_context(self, data, accepted_media_type, renderer_context):
"""
Expand Down Expand Up @@ -672,8 +672,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):

template = loader.get_template(self.template)
context = self.get_context(data, accepted_media_type, renderer_context)
context = RequestContext(renderer_context['request'], context)
ret = template.render(context)
ret = template_render(template, context, request=renderer_context['request'])

# Munge DELETE Response code to allow us to return content
# (Do this *after* we've rendered the template so that we include
Expand Down Expand Up @@ -709,8 +708,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):

template = loader.get_template(self.template)
context = self.get_context(data, accepted_media_type, renderer_context)
context = RequestContext(renderer_context['request'], context)
ret = template.render(context)
ret = template_render(template, context, request=renderer_context['request'])

# Creation and deletion should use redirects in the admin style.
if (response.status_code == status.HTTP_201_CREATED) and ('Location' in response):
Expand Down
11 changes: 6 additions & 5 deletions rest_framework/templatetags/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

from django import template
from django.core.urlresolvers import NoReverseMatch, reverse
from django.template import Context, loader
from django.template import loader
from django.utils import six
from django.utils.encoding import force_text, iri_to_uri
from django.utils.html import escape, format_html, smart_urlquote
from django.utils.safestring import SafeData, mark_safe

from rest_framework.compat import template_render
from rest_framework.renderers import HTMLFormRenderer
from rest_framework.utils.urls import replace_query_param

Expand Down Expand Up @@ -128,12 +129,12 @@ def format_value(value):
template = loader.get_template('rest_framework/admin/list_value.html')
else:
template = loader.get_template('rest_framework/admin/simple_list_value.html')
context = Context({'value': value})
return template.render(context)
context = {'value': value}
return template_render(template, context)
elif isinstance(value, dict):
template = loader.get_template('rest_framework/admin/dict_value.html')
context = Context({'value': value})
return template.render(context)
context = {'value': value}
return template_render(template, context)
elif isinstance(value, six.string_types):
if (
(value.startswith('http:') or value.startswith('https:')) and not
Expand Down