[英]Dynamic tables with Django tables2
I'm using the django_tables2 package to display tables on my page. 我正在使用django_tables2包在我的页面上显示表。 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: 在我的views.py中,我有:
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 TableA和TableB在我的table.py中定义
In my templatetabs folder I have tags.py: 在我的templatetabs文件夹中,我有tags.py:
@register.assignment_tag
def get_table(table, primary_key):
return table.get(primary_key)
Finally in my index.html: 最后在我的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. 我在pycharm中运行django 1.11,当我在pycharm中运行时,这很好。 When I run this on a debian server I get en error.
当我在Debian服务器上运行此程序时,出现错误。 If anything is passed into primary_key, tables_A, and tables_B I get a 500 Internal Server Error.
如果将任何内容传递给primary_key,tables_A和tables_B,则会收到500 Internal Server Error。 If those dictionaries are empty I get: 'Invalid block tag on line 19: 'get_table', expected 'empty' or 'endfor'.
如果这些字典为空,我得到:'第19行的块标记无效:'get_table',预期为'empty'或'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?
还是有更好的方法来做到这一点?
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 %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.