Opened 6 years ago

Closed 6 years ago

#29960 closed Bug (invalid)

`render_to_string` passes `None` for `context` if unspecified, but `Template.render` expects type `Context`

Reported by: Joshua Cannon Owned by: nobody
Component: Template system Version: 2.1
Severity: Normal Keywords: render_to_string, template, context
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Tim Graham, 6 years ago

Easy pickings: unset

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.

comment:2 by Joshua Cannon, 6 years ago

Resolution: invalid
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top