【问题标题】:How to fill a Django table with non-query data?如何用非查询数据填充 Django 表?
【发布时间】:2017-08-09 03:00:42
【问题描述】:

我想知道是否有人知道用 python 文件而不是查询集中的数据填充 Django 表的任何方法?

【问题讨论】:

  • @DBrowne 是的。这就是我构建表格的方式。但是,这一次,我想用 python 文件中的一些计算数据更新我的表。所以,我想知道正确的设置是什么。

标签: python html django django-tables2


【解决方案1】:

将其添加到视图的上下文中;假设您有一个由名为“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>

我还建议您更具体地提出问题;提供所有相关代码会更有用,人们可以为您的问题提供具体答案。

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 2014-02-14
    • 2013-06-09
    • 1970-01-01
    • 2020-05-26
    • 2019-09-10
    • 2019-01-17
    • 2013-07-30
    • 2014-03-31
    相关资源
    最近更新 更多