Thursday, March 22, 2018

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

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
<christian.ledermann@gmail.com> wrote:
> 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.
>
> On 5 March 2018 at 05:50, Bernd Wechner <bernd.wechner@gmail.com> wrote:
>> Can't say I'm experienced with template.Node, but you piqued my curiosity so
>> I found this:
>>
>> https://stackoverflow.com/questions/14434624/how-to-get-dictionary-of-requestcontext-imported-from-django-template
>>
>> 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.
>>
>>
>> On Saturday, 3 March 2018 06:26:46 UTC+11, Christian Ledermann wrote:
>>>
>>> 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.
>>>
>>> }<(((*>
>>
>> --
>> 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+unsubscribe@googlegroups.com.
>> To post to this group, send email to django-users@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.
>
>
>
> --
> 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.
>
> }<(((*>



--
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.

}<(((*>

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CABCjzWrK5-juZoJ7AVWVt5h%3DFO5U1z2g%3DRm1ibjvDHiGB7MTZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment