jieter/django-tables2

Use of context in a TemplateColumn

Closed this issue · 0 comments

Hi,

I'm trying to create a TemplateColumn which needs the context.get('request') to work, as some data rendering is based on the connected user. I've read the docs...

Table loading

I provide the request to the table constructor.

views.py

from .tables import MyTable

class MyDetailView(DetailView):
    ....
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['table'] = MyTable(some_data, request=self.request)
        return context

Table definition

tables.py

class MyTable(Table):
    my_col = TemplateColumn(verbose_name="", template_name="my_template.html")

Template

I use a simple tag a in the template of a TemplateColumn.

my_template.html

{% load custom_tags %}
{% my_custom_tag record %}

Template tag:

The tag has the takes_context=True parameter.

custom_tags.py

@register.simple_tag(takes_context=True)
def my_custom_tag(context, instance):
    request = context.get("request")  # Does not work !?

Then when I render the table, I do not have the request object in the rendering context.

What did I miss ?

Thanks !