#28491: TypeError: context must be a dict rather than Context.
-------------------------------------------+------------------------
Reporter: Mark Jones | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 1.11
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 render method of the Template class looks like this
{{{
def render(self, context=None, request=None):
context = make_context(context, request,
autoescape=self.backend.engine.autoescape)
try:
return self.template.render(context)
except TemplateDoesNotExist as exc:
reraise(exc, self.backend)
}}}
The docs show this:
{{{
>>> from django.template import Context, Template
>>> template = Template("My name is {{ my_name }}.")
>>> context = Context({"my_name": "Adrian"})
>>> template.render(context)
"My name is Adrian."
>>> context = Context({"my_name": "Dolores"})
>>> template.render(context)
"My name is Dolores."
}}}
However make_context looks like this (and throws an exception because of
it):
{{{
if context is not None and not isinstance(context, dict):
raise TypeError('context must be a dict rather than %s.' %
context.__class__.__name__)
if request is None:
context = Context(context, **kwargs)
else:
# The following pattern is required to ensure values from
# context override those from template context processors.
original_context = context
context = RequestContext(request, **kwargs)
if original_context:
context.push(original_context)
return context
}}}
I think it needs this code placed before the original code in make_context
{{{
if isinstance(context, Context):
return context
}}}
so that if an actual context is passed in, it works as documented.
This bug only comes about when you select a template using code that looks
like this:
{{{
from django.template import loader, Context
servertemplate = loader.select_template(["deployment/%s" %
options['webserver'],
"deployment/default_%s" % options['webserver']])
}}}
because the loader returns an instance of **<class
'django.template.backends.django.Template'>** instead of **<class
'django.template.base.Template'>** which has a different render method
that causes this problem.
My change does fix the issue, but I'm not sure if this is the right fix,
or if there should be another.
--
Ticket URL: <https://code.djangoproject.com/ticket/28491>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.7b882ca5debecb2cce4dc0c577ea1bc3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.