Add and switch to new template compatibility functions for Django 1.8+.

Review Request #9659 — Created Feb. 17, 2018 and submitted




Django 1.8 introduced a concept of template backends and refined many
template-related functions, such as render_to_string. Throughout 1.10,
many of these functions retained deprecated compatibility with older
function signatures, but Django 1.11 dropped that support.

One of the major changes is that render_to_string() and
render_to_response() no longer takes a Context or RequestContext
object, but rather expects a dictionary and optional HttpRequest,
allowing it to determine the correct context object. Similarly,
Template.render no longer takes these context objects.

Part of this was easy to update. Django's been providing a render()
function that easily replaces render_to_response() when used with
a RequestContext, and its function signature has remained compatible
ever since. The rest required some new cross-Django compatibility
functions that allow us to migrate code to the new call signatures.

djblets.util.compat.template.loader.render_to_string() looks like the
modern version in Django, but accepts old and new call sites, converting
appropriately for the correct version of Django.

djblets.util.compat.template.loader.render_template() wraps
Template.render() in the same way, but is a standalone function taking
a Template instance.

djblets.util.compat.template.context.flatten_context() is a standalone
function that performs the task of Context.flatten() (which was
introduced in Django 1.7).

There's also a new module, djblets.template.context, which contains
the new get_default_template_context_processors(). This is a safer way
for callers (such as unit tests) to fetch the list of default template
context processors. This was added as a formal function instead of a
compatibility function because it's useful enough by itself to remain
after we eventually move away from all the new compatibility modules,
given the non-obvious work it must do to return a result on newer
versions of Django.

This all brings us a step closer to Django 1.11 support, allowing the
template-related unit tests to pass (in combination with other changes
that will be posted soon).

All unit tests pass on Django 1.7.

Unit tests pass on Django 1.11 (in combination with another set of
local changes not part of this commit).

Description From Last Updated

F401 'djblets.util.compat.django.template.context.flatten_context' imported but unused


F821 undefined name 'ImproperlyConfigured'


F401 'djblets.util.compat.django.template.context.flatten_context' imported but unused

Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.


  1. Great cleanup!

Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-1.0.x (5b4eac8)