Django 1,11 upgrade, render template with context in template.Node

119 views
Skip to first unread message

Christian Ledermann

unread,
Mar 2, 2018, 2:26:46 PM3/2/18
to django...@googlegroups.com
In Django 1,8 it worked but in Django 1.11 it throws:

TypeError: context must be a dict rather than RequestContext.

The code is:

class EpilogueNode(template.Node):

def __init__(self): # noqa: D102
self.epilogue_template =
template.loader.get_template('themes/presenter_epilogue.html')

def render(self, context): # noqa: D102
rendered = self.epilogue_template.render(context)

In https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/ it
is documeted that I need to pass a dict as context, but the context
passed to Node,render is a
RequestContext.

How can I upgrade this to 1.11?



--
Best Regards,

Christian Ledermann

Newark-on-Trent - UK
Mobile : +44 7474997517

https://uk.linkedin.com/in/christianledermann
https://github.com/cleder/


<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don’t drive species to extinction

2) Don’t destroy a habitat that species rely on.

3) Don’t change the climate in ways that will result in the above.

}<(((*>

Bernd Wechner

unread,
Mar 5, 2018, 12:50:55 AM3/5/18
to Django users
Can't say I'm experienced with template.Node, but you piqued my curiosity so I found this:


which may help, but am left wondering why upgrade to 1.11 when 2.0 is out already and the way to go? I'm on 1.11 and about to move to 2.0.

Regards,

Bernd.

Christian Ledermann

unread,
Mar 5, 2018, 3:50:51 AM3/5/18
to django...@googlegroups.com
cool thanks I try that :-)
Django 1.11 vs 2.x => we are still on python 2.7 so the first step is
Django 1.11 with python 2.7, that 1.11 upgrade to python 3.x and
afterwards (well you get the timeline)
Our codebase is 11 years old so there are some stumble blocks upgrading.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0d289efe-614b-4fa8-ad87-87d559b6aaa3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Christian Ledermann

unread,
Mar 22, 2018, 12:17:10 PM3/22/18
to django...@googlegroups.com
For the record,
I ended up with:

def get_context_dict_request(context):
"""Decompose a Context or a RequestContext to a request and dict."""
ctx = context.flatten()
try:
request = context.request
except AttributeError:
request = None
return ctx, request

class EpilogueNode(template.Node):

def __init__(self): # noqa: D102
self.epilogue_template =
template.loader.get_template('themes/presenter_epilogue.html')

def render(self, context): # noqa: D102
context, request = get_context_dict_request(context)
rendered = self.epilogue_template.render(context=context,
request=request)

On 5 March 2018 at 08:49, Christian Ledermann
Reply all
Reply to author
Forward
0 new messages