[Django] #29960: `render_to_string` passes `None` for `context` if unspecified, but `Template.render` expects type `Context`

3 views
Skip to first unread message

Django

unread,
Nov 16, 2018, 1:06:51 PM11/16/18
to django-...@googlegroups.com
#29960: `render_to_string` passes `None` for `context` if unspecified, but
`Template.render` expects type `Context`
-------------------------------------+-------------------------------------
Reporter: Joshua | Owner: nobody
Cannon |
Type: Bug | Status: new
Component: Template | Version: 2.1
system | Keywords: render_to_string,
Severity: Normal | template, context
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
I'm not sure if this is a bug bug or just misleading documentation, but
the signature/docstring for `render_to_string` starts as follows:
{{{
def render_to_string(template_name, context=None, request=None,
using=None):
"""
Load a template and render it with a context. Return a string.
...
}}}

The docstring would lead the reader to believe the context isn't optional.
If the caller omits `context`, they will likely get the following
exception:
{{{AttributeError: 'NoneType' object has no attribute 'render_context'}}}

which is due to `Template.render`'s first line being:
{{{
def render(self, context):
with context.render_context.push_state(self):
...
}}}

The signature of `Template.render` would lead me to believe context is not
optional and must be of type `Context`.

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

Django

unread,
Nov 16, 2018, 2:30:20 PM11/16/18
to django-...@googlegroups.com
#29960: `render_to_string` passes `None` for `context` if unspecified, but
`Template.render` expects type `Context`
-------------------------------------+-------------------------------------
Reporter: Joshua Cannon | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 2.1
Severity: Normal | Resolution:
Keywords: render_to_string, | Triage Stage:
template, context | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* easy: 1 => 0


Comment:

Please give code to reproduce the problem. There are `render_to_string()`
calls in `tests/test_utils/tests.py`, for example, that omit context
without a problem.

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

Django

unread,
Nov 16, 2018, 2:38:25 PM11/16/18
to django-...@googlegroups.com
#29960: `render_to_string` passes `None` for `context` if unspecified, but
`Template.render` expects type `Context`
-------------------------------------+-------------------------------------
Reporter: Joshua Cannon | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: render_to_string, | Triage Stage:
template, context | Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Comment:

The type of the template that `render_to_string` calls `render` on is
`django.template.backends.django.Template`, not `django.template.Template`
(which was quoted).

Sorry for the noise.

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

Reply all
Reply to author
Forward
0 new messages