Skip to content
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

Fix popping the extra_context of TemplateColumn #767

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,19 @@ def render(self, record, table, value, bound_column, **kwargs):
# If the table is being rendered using `render_table`, it hackily
# attaches the context to the table as a gift to `TemplateColumn`.
context = getattr(table, "context", Context())
context.update(self.extra_context)
context.update(
{
"default": bound_column.default,
"column": bound_column,
"record": record,
"value": value,
"row_counter": kwargs["bound_row"].row_counter,
}
)

try:
additional_context = {
"default": bound_column.default,
"column": bound_column,
"record": record,
"value": value,
"row_counter": kwargs["bound_row"].row_counter,
}
additional_context.update(self.extra_context)
with context.update(additional_context):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not know about the ability to use Context.update() as a context manager, nice!

if self.template_code:
return Template(self.template_code).render(context)
else:
return get_template(self.template_name).render(context.flatten())
finally:
context.pop()

def value(self, **kwargs):
"""
Expand Down