Search code examples
pythondjangodjango-templatesdjango-viewsdjango-tables2

Dynamic tables with Django tables2


I'm using the django_tables2 package to display tables on my page. I have two sets of data which I want to display dynamically.

<Table_1_A>
<Table_1_B>

<Table_2_A>
<Table_2_B>

<Table_n_A>
<Table_n_B>

In my views.py I have:

primary_keys = {1, 2, ... n} # these are not simple or ordered integers
tables_A = {}
tables_B = {}

for primary_key in primary_keys:
    tables_A['primary_key'] = TableA(table_A_queryset.filter(pk=primary_key))
    RequestConfig(request).configure(tables_A[primary_key])

    tables_B['primary_key'] = TableB(table_B_queryset.filter(pk=primary_key))
    RequestConfig(request).configure(tables_B[primary_key])

return render(request, 'index.html', {'primary_keys': primary_keys, 'tables_A ': tables_A , 'tables_B ': tables_B })

TableA and TableB are defined in my tables.py

In my templatetabs folder I have tags.py:

@register.assignment_tag
    def get_table(table, primary_key):
return table.get(primary_key)

Finally in my index.html:

{% load render_table from django_tables2 %}
{% load tags %}

{% block content %}

    {% for primary_key in primary_keys %}
         <div class="data_tables">
            {% get_table tables_A primary_key as table_A %}
            {% render_table table_A %}
            <br>
            <br>

            {% get_table tables_B primary_key as table_B%}
            {% render_table table_B%}
        </div>
    {% endfor %}

{% endblock content %}

I'm running django 1.11 in pycharm and when I'm running in pycharm this works just fine. When I run this on a debian server I get en error. If anything is passed into primary_key, tables_A, and tables_B I get a 500 Internal Server Error. If those dictionaries are empty I get: 'Invalid block tag on line 19: 'get_table', expected 'empty' or 'endfor'. Did you forget to register or load this tag?'

Any reason this doesn't work on a server but does locally? Or is there a better way to do this?


Solution

  • I was able to solve this using lists instead of dictionaries.

    primary_keys = [1, 2, ... n] # these are not simple or ordered integers
    tables_A = []
    tables_B = []
    
    for idx, primary_key in enumerate(primary_keys):
        tables_A.append(TableA(table_A_queryset.filter(pk=primary_key)))
        RequestConfig(request).configure(tables_A[idx])
    
        tables_B.append(TableB(table_B_queryset.filter(pk=primary_key)))
        RequestConfig(request).configure(tables_B[idx])
    
    return render(request, 'index.html', {'tables_A ': tables_A , 'tables_B ': tables_B })
    

    I did not need to use the template tags at all

    {% load render_table from django_tables2 %}
    
    {% block content %}
    
        {% for val in table_A %}
             <div class="data_tables">
                {% render_table table_A.pop %}
                <br>
                <br>
                {% render_table table_B.pop %}
            </div>
        {% endfor %}
    
    {% endblock content %}