clokep / django-render-block

Render the content of a specific block tag from a Django template.
ISC License
241 stars 23 forks source link

TemplateNotFound errors when the same context is passed to django_render_block multiple times #23

Closed evanbrumley closed 4 years ago

evanbrumley commented 4 years ago

As of 0.8, django_render_block(...) was modified to preserve the original context object if it had the type django.template.Context. Unfortunately, context objects are mutable and this has resulted in odd behaviour if django_render_block is called multiple times with the same context object. In our case, we started seeing TemplateNotFound errors because Django's templating engine skipped templates it thought it had already parsed.

This is a somewhat urgent issue, because django-templated-email has an unpinned dependency on django-render-block and calls django_render_block multiple times with the same context: https://github.com/vintasoftware/django-templated-email/blob/cba05a7b0f355acdc18174acd1ae468ad77e023d/templated_email/backends/vanilla_django.py#L106

The solution should be as simple as making a deep copy of the context object instead of using it directly.

clokep commented 4 years ago

Yeah, I think we'll need to do a point release then. I can try to look at this in the next few days, but if you're able to put up a PR I'd appreciate it!

clokep commented 4 years ago

@evanbrumley I think that #24 will fix this, but I wasn't able to create a reduced test-case. Re-using a context for me caused no issues.

Do you have a reduced test-case for this?

clokep commented 4 years ago

At the very least it would be nice to know if that branch fixes the issue. 😄

evanbrumley commented 4 years ago

@clokep Have just created PR #25, which contains a fix and a test that should demonstrate the issue. Note that an additional step was required after the copy, because Django explicitly tries to preserve the rendering context between copies.

clokep commented 4 years ago

Thanks! I'll take a look at this soon. Oddly the docs don't give any warning about not re-using these. 😕

clokep commented 4 years ago

I just pushed 0.8.1 which includes the fix. Thanks so much! 👍

evanbrumley commented 4 years ago

No problems! Thank you for taking the time to maintain such a useful library :-)