[Django] #23789: TemplateResponse handles context/context processors differently from 'render' shortcut

28 views
Skip to first unread message

Django

unread,
Nov 10, 2014, 4:53:30 AM11/10/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
-------------------------------------------+------------------------
Reporter: spookylukey | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
The documentation for `TemplateResponse` states that `TemplateResponse`
can be used as an alternative to `render_to_response`. (In fact, it would
be better to point out that it is a closer equivalent to `render`).

https://docs.djangoproject.com/en/dev/ref/template-response/#using-
templateresponse-and-simpletemplateresponse

Similarly, the docs for `render` suggest that the `TemplateResponse`
constructor can be used as a drop in equivalent: "the constructor of
TemplateResponse offers the same level of convenience as render()"

https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render

There is a subtle but significant difference, however:

If a context processor returns a dictionary with a key that is the same as
one in the context dictionary passed in to the `TemplateResponse`
constructor, then the priority is different: `TemplateResponse` will
overwrite (or shadow) the passed in context dictionary using the data from
the context processors, while `render` does it the other way around.

In my opinion, the behaviour in `render` is much more useful: there are a
number of circumstances when I might use a context processor to define
some default global data for templates, but then I might want to override
that in a specific view.

There is value the other way around, of course: if a 3rd party app
provides puts some data into a context that you want to change by
configuring a context processor. However, the whole point of
`TemplateResponse` is to make it easy to make changes like that in other
ways - a simple view wrapper or middleware will allow you to do it. The
behaviour of `render` is also older and therefore much more likely to have
patterns established around it.

I'd like us fix `TemplateResponse` to be in line with `render`, as it
represents a significant gotcha if you transition from `render` to
`TemplateResponse`. (In one project I've had to fix it using a custom
`TemplateResponse` subclass, but it took me a good while to debug the
problem). This, of course, is a backwards incompatible change for the
case where someone was relying on the current behaviour. However, given
that we do not currently test or document the current behaviour of
`TemplateResponse` in this regard, and the docs suggest the opposite (you
can use the `TemplateResponse` constructor as a way to get the behaviour
of `render` but with a `TemplateResponse` instance returned instead of an
`HttpResponse`), I think we can easily argue that the current behaviour
of `TemplateResponse` is a bug that should be fixed.

I'll attach a patch that implements it for reference, which is very
simple. Note that apart from the new test I've added, there are no
failures in our test suite caused by the change in behaviour i.e. nothing
in Django itself depends on the current behaviour. Obviously I'll add a
backwards compatibility note if others agree with this change, and then
commit.

--
Ticket URL: <https://code.djangoproject.com/ticket/23789>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 10, 2014, 4:54:56 AM11/10/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
---------------------------------+--------------------------------------

Reporter: spookylukey | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by spookylukey):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/23789#comment:1>

Django

unread,
Nov 10, 2014, 5:13:33 AM11/10/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
---------------------------------+------------------------------------

Reporter: spookylukey | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by aaugustin):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/23789#comment:2>

Django

unread,
Nov 10, 2014, 9:49:44 AM11/10/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
---------------------------------+------------------------------------
Reporter: spookylukey | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.7
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Luke Plant <L.Plant.98@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"b748a8bc670af10c37560836c353ce911eaeecc0"]:
{{{
#!CommitTicketReference repository=""
revision="b748a8bc670af10c37560836c353ce911eaeecc0"
Fixed #23789 -- TemplateResponse handles context differently from render
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23789#comment:3>

Django

unread,
Dec 17, 2014, 5:14:55 PM12/17/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
---------------------------------+------------------------------------

Reporter: spookylukey | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by aaugustin):

* status: closed => new
* resolution: fixed =>


Comment:

The following paragraph no longer applies after this change:

https://github.com/django/django/blob/b748a8bc670af10c37560836c353ce911eaeecc0/docs/ref
/class-based-views/mixins-simple.txt#L61-L70

It should be removed from the documentation.

--
Ticket URL: <https://code.djangoproject.com/ticket/23789#comment:4>

Django

unread,
Dec 17, 2014, 8:04:16 PM12/17/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
---------------------------------+------------------------------------

Reporter: spookylukey | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"493ab45349e7768cba137207b8a27282921e9388"]:
{{{
#!CommitTicketReference repository=""
revision="493ab45349e7768cba137207b8a27282921e9388"
Updated TemplateResponseMixin docs to reflect new behavior after refs
#23789.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23789#comment:5>

Django

unread,
Dec 17, 2014, 8:04:45 PM12/17/14
to django-...@googlegroups.com
#23789: TemplateResponse handles context/context processors differently from
'render' shortcut
---------------------------------+------------------------------------
Reporter: spookylukey | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.7
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/23789#comment:6>

Reply all
Reply to author
Forward
0 new messages