#27722: if a template context is an instance of get_template(), it will raise
"TypeError: context must be a dict rather than RequestContext"
---------------------------------+--------------------------------------
Reporter: Sayid Munawar | Owner: nobody
Type: Bug | Status: closed
Component: Template system | Version: master
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by Sayid Munawar:
Old description:
> This simple view:
> {{{
> class Ngopi(TemplateView):
> template_name = 'base.html'
>
> def get_context_data(self, **kwargs):
> context = super(Ngopi, self).get_context_data(**kwargs)
> context.update({
> 'menu': get_template('menu.html')
> })
> return context
> }}}
>
> with `base.html`:
> {{{
> <html>
> <body>
> <h1>Ngeteh</h1>
> {% include menu %}
> </body>
> </html>
> }}}
>
> and `menu.html`:
> {{{
> <ul>
> <li>Ngeteh</li>
> <li>Ngopi</li>
> </ul>
> }}}
>
> This situation will raise this exception:
> {{{
> Traceback (most recent call last):
> File
> "/Users/ayik/Repo/Django/django/django/core/handlers/exception.py", line
> 38, in inner
> response = get_response(request)
> File "/Users/ayik/Repo/Django/django/django/core/handlers/base.py",
> line 217, in _get_response
> response = self.process_exception_by_middleware(e, request)
> File "/Users/ayik/Repo/Django/django/django/core/handlers/base.py",
> line 215, in _get_response
> response = response.render()
> File "/Users/ayik/Repo/Django/django/django/template/response.py", line
> 107, in render
> self.content = self.rendered_content
> File "/Users/ayik/Repo/Django/django/django/template/response.py", line
> 84, in rendered_content
> content = template.render(context, self._request)
> File
> "/Users/ayik/Repo/Django/django/django/template/backends/django.py", line
> 66, in render
> return self.template.render(context)
> File "/Users/ayik/Repo/Django/django/django/template/base.py", line
> 207, in render
> return self._render(context)
> File "/Users/ayik/Repo/Django/django/django/template/base.py", line
> 199, in _render
> return self.nodelist.render(context)
> File "/Users/ayik/Repo/Django/django/django/template/base.py", line
> 990, in render
> bit = node.render_annotated(context)
> File "/Users/ayik/Repo/Django/django/django/template/base.py", line
> 957, in render_annotated
> return self.render(context)
> File "/Users/ayik/Repo/Django/django/django/template/loader_tags.py",
> line 212, in render
> return template.render(context)
> File
> "/Users/ayik/Repo/Django/django/django/template/backends/django.py", line
> 64, in render
> context = make_context(context, request,
> autoescape=self.backend.engine.autoescape)
> File "/Users/ayik/Repo/Django/django/django/template/context.py", line
> 285, in make_context
> raise TypeError('context must be a dict rather than %s.' %
> context.__class__.__name__)
> TypeError: context must be a dict rather than RequestContext.
> }}}
>
> This is fine with django 1.10, but not master (mine is currently
> 1.11.dev20170109230310)
>
> a quick workaround is to edit
> [https://github.com/django/django/blob/ecb59cc6579402b68ddfd4499bf30edacf5963be/django/template/backends/django.py#L63
> django/template/backends/django.py Line 63] to be like this:
> {{{
> def render(self, context=None, request=None):
> if isinstance(context, dict): # <-- my temporary workaround
> context = make_context(context, request,
> autoescape=self.backend.engine.autoescape)
> try:
> return self.template.render(context)
> except TemplateDoesNotExist as exc:
> reraise(exc, self.backend)
> }}}
New description:
This simple view:
{{{
from django.views.generic import TemplateView
from django.template.loader import get_template
class Ngopi(TemplateView):
template_name = 'base.html'
def get_context_data(self, **kwargs):
context = super(Ngopi, self).get_context_data(**kwargs)
context.update({
'menu': get_template('menu.html')
})
return context
}}}
with `base.html`:
{{{
<html>
<body>
<h1>Ngeteh</h1>
{% include menu %}
</body>
</html>
}}}
and `menu.html`:
{{{
<ul>
<li>Ngeteh</li>
<li>Ngopi</li>
</ul>
}}}
This situation will raise this exception:
{{{
Traceback (most recent call last):
File "/Users/ayik/Repo/Django/django/django/core/handlers/exception.py",
line 38, in inner
response = get_response(request)
File "/Users/ayik/Repo/Django/django/django/core/handlers/base.py", line
217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/ayik/Repo/Django/django/django/core/handlers/base.py", line
215, in _get_response
response = response.render()
File "/Users/ayik/Repo/Django/django/django/template/response.py", line
107, in render
self.content = self.rendered_content
File "/Users/ayik/Repo/Django/django/django/template/response.py", line
84, in rendered_content
content = template.render(context, self._request)
File
"/Users/ayik/Repo/Django/django/django/template/backends/django.py", line
66, in render
return self.template.render(context)
File "/Users/ayik/Repo/Django/django/django/template/base.py", line 207,
in render
return self._render(context)
File "/Users/ayik/Repo/Django/django/django/template/base.py", line 199,
in _render
return self.nodelist.render(context)
File "/Users/ayik/Repo/Django/django/django/template/base.py", line 990,
in render
bit = node.render_annotated(context)
File "/Users/ayik/Repo/Django/django/django/template/base.py", line 957,
in render_annotated
return self.render(context)
File "/Users/ayik/Repo/Django/django/django/template/loader_tags.py",
line 212, in render
return template.render(context)
File
"/Users/ayik/Repo/Django/django/django/template/backends/django.py", line
64, in render
context = make_context(context, request,
autoescape=self.backend.engine.autoescape)
File "/Users/ayik/Repo/Django/django/django/template/context.py", line
285, in make_context
raise TypeError('context must be a dict rather than %s.' %
context.__class__.__name__)
TypeError: context must be a dict rather than RequestContext.
}}}
This is fine with django 1.10, but not master (mine is currently
1.11.dev20170109230310)
a quick workaround is to edit
[https://github.com/django/django/blob/ecb59cc6579402b68ddfd4499bf30edacf5963be/django/template/backends/django.py#L63
django/template/backends/django.py Line 63] to be like this:
{{{
def render(self, context=None, request=None):
if isinstance(context, dict): # <-- my temporary workaround
context = make_context(context, request,
autoescape=self.backend.engine.autoescape)
try:
return self.template.render(context)
except TemplateDoesNotExist as exc:
reraise(exc, self.backend)
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27722#comment:5>
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/065.519d73b9ce03add56011372c716dbd12%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.