小编Jos*_*oni的帖子

如何使用 django-tables2 在一个视图中显示多个表?

使用 djt2 v0.15/dj1.6/pyth2.6.6

多个表的 djt2 doc 视图文件示例:

def people_listing(request) :

    config = RequestConfig(request)
    table1 = PeopleTable(Person.objects.all(), prefix="1-")
    table2 = PeopleTable(Person.objects.all(), prefix="2-")
    config.configure(table1)
    config.configure(table2)
    return render(request, "people_listing.html",
        {"table1": table1, "table2": table2})
Run Code Online (Sandbox Code Playgroud)

首先,该示例对于引用的“table1”、“table2”参数似乎不正确。我的测试显示定义名称“people_list”需要在引号中使用,至少在单个表上。此外,为什么有人想将同一张表显示两次?这是一个坏例子吗?这是我的应用程序尝试使用这种结构:

def AvLabVw(request):

    config = RequestConfig(request)
    cmutbl = CmuTable(CmuVersion.objects.all(), prefix="1-")
    simtbl = SimTable(Simulator.objects.all(), prefix="2-")
    config.configure(cmutbl)
    config.configure(simtbl)
    return render(request, "AvRelInfo.html", {"AvLabVw":cmutbl, "AvLabVw":simtbl})
Run Code Online (Sandbox Code Playgroud)

url 文件在 AvLabVw 上选取,html 模板使用 render_table。

{% render_table AvLabVw %}
Run Code Online (Sandbox Code Playgroud)

此代码所发生的情况是仅仍然显示一个表,以返回渲染行上的最后一个表为准。

在文档的其他地方,它说需要使用具有 get_context_data 的 SingleTableView ,我还没有弄清楚......

我尝试过这种风格的实现,我认为它需要一个表对象和一个列表对象?

视图.py

从 django_tables2 导入视图
从 django_tables2 导入 SingleTableView
从 django_tables2 导入 SingleTableMixin …

python view multiple-tables django-tables2

2
推荐指数
1
解决办法
9453
查看次数

标签 统计

django-tables2 ×1

multiple-tables ×1

python ×1

view ×1