-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
render_table tag overwrites "table" variable from outer context #547
Comments
The comments here suggest the added entry is popped off. Maybe something changed in how django handles this and there clearly is no test checking for it. I don't think we really need the original context, except for |
@thunderk I tried reproducing the issue with this test, but it seems to pass without changes. def test_does_not_touch_context(self):
'''
Make sure the tag does not change the context of the template the tag is called from
https://github.com/jieter/django-tables2/issues/547
'''
table = CountryTable([], template_name='dummy.html')
template = Template(
'{% load django_tables2 %}'
'{% with "foo" as table %}{{ table }} {% render_table mytable %} {{ table }}{% endwith %}'
)
html = template.render(Context({
'request': build_request(),
'mytable': table,
}))
self.assertEqual(html, 'foo dummy template contents\n foo') Can you spot the difference between your case and my test? |
Fyi, this is a commit with change which would fix this problem. But the test also passes without the fix. |
Sorry about the delay, but I'm struggling to reproduce the bug on a small case (it happens in our quite large app, with several levels of template inheritance and custom tags). |
Ok, got it, the table has to be non-empty to trigger the bug: def test_does_not_touch_context(self):
'''
Make sure the tag does not change the context of the template the tag is called from
https://github.com/jieter/django-tables2/issues/547
'''
class TestTable(Table):
col = TemplateColumn(template_name='test_template_column.html')
table = TestTable([{}])
template = Template(
'{% load django_tables2 %}'
'{% with "foo" as table %}{{ table }} {% render_table mytable %} {{ table }}{% endwith %}'
)
html = template.render(Context({
'request': build_request(),
'mytable': table,
}))
self.assertEqual(html[:4], 'foo ')
self.assertEqual(html[-4:], ' foo') |
@jieter Yes, case solved ! Thank you. |
released 1.21.0 |
The render_table tag binds its first argument (the Table object) to a table variable, which is then used in child template for rendering.
The problem is that this seems to overwrite any previous table variable set in the parent context.
Here is a small example, with a django-tables2 Table object in mytable template variable:
The output of this template is:
Tested with version 1.17.1
The text was updated successfully, but these errors were encountered: