I am rendering a few tables which I add to the context in the view
from .models import MyModel
from .tables import MyModelTable
def index(request):
context = dict(all_tables=[])
template = 'mypage/index.html'
for x in some_list:
if some_condition(x):
context[x] = MyModelTable(get_some_data(x))
context['all_tables'].append(x)
context['all_tables'] = sort_my_way(context['all_tables'])
return render(request, template, context)
And then I try to iterate over the list and create the tables on by one. However, I can't figure out how to get the table from the context using the string name.
index.html
{% load django_tables2 %}
{% load render_table from django_tables2 %}
<!doctype html>
<html>
<link rel="stylesheet" href="{% static 'css/my.css' %}" />
<body>
{% for t in all_tables %}
{% if t %}
<H3>{{ t }}</H3>
{% render_table t %} <--- How can I get the table with name t from context
<br/>
{% endif %}
{% endfor %}
</body>
What I am trying to do here is saving myself from having a huge list of those here in my index.html
{% if TABLE_1 %}
<H3>TABLE_1 </H3>
{% render_table TABLE_1 %}
<br/>
{% endif %}
....
{% if TABLE_N %}
<H3>TABLE_N </H3>
{% render_table TABLE_N %}
<br/>
{% endif %}