【发布时间】:2017-08-09 03:00:42
【问题描述】:
我想知道是否有人知道用 python 文件而不是查询集中的数据填充 Django 表的任何方法?
【问题讨论】:
-
@DBrowne 是的。这就是我构建表格的方式。但是,这一次,我想用 python 文件中的一些计算数据更新我的表。所以,我想知道正确的设置是什么。
标签: python html django django-tables2
我想知道是否有人知道用 python 文件而不是查询集中的数据填充 Django 表的任何方法?
【问题讨论】:
标签: python html django django-tables2
将其添加到视图的上下文中;假设您有一个由名为“data_function”的函数生成的 dict 对象列表,准备好在名为“data.py”的文件中为您的表传递:
from .data import data_function as data_for_table
def fill_table(request):
context = {
'data_for_table': data_for_table()
}
return render(request, 'app/table-template.html', context)
这样您就可以从您的模板中执行以下操作:
<table>
<tr>
<th>data1</th>
<th>data1</th>
</tr>
{% for data in data_for_table %}
<tr>
<th>{{ data.data1 }}</th>
<th>{{ data.data2 }}</th>
</tr>
{% endfor %}
</table>
我还建议您更具体地提出问题;提供所有相关代码会更有用,人们可以为您的问题提供具体答案。
【讨论】: