在 django_tables2 中转置 Django 表
我正在尝试创建一个视图,该视图本质上显示有关此类记录的信息
|Description| blahblahblah
|Name | blah blah blahs
|Last Edited| someDate
|Owned by | Blah blah blah info
,该记录是从 django_tables2 默认呈现表的方式转置的。有没有一种简单的方法可以做到这一点,或者我必须编写自己的模板?我发现这样做(理论上)是因为有一个自定义模板的好例子,它说我必须“将 Table 子类的实例传递到您自己的模板中,然后自己渲染它”。我实际上不知道这是什么意思:(
I'm trying to make a view that essentially shows information about a record like this
|Description| blahblahblah
|Name | blah blah blahs
|Last Edited| someDate
|Owned by | Blah blah blah info
which is transposed from the way that django_tables2 renders tables by default. Is there an easy way to do this or will I have to code my own template? I'm find doing that (in theory) as there's a good example of a custom template which says I have to "pass an instance of your Table subclass into your own template, and render it yourself". I don't actually know what's meant by that though :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要编写自己的模板,通过迭代表的属性并编写正确的标签来生成所需的 HTML。用于说明如何执行此操作的一个很好的示例是用于
as_html()
函数的模板:https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.htmlYou will need to write your own template that produces the HTML you desire, by iterating over attributes of the table and writing the correct tags. A good example of how you might do this is the template used for the
as_html()
function: https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html我今天在一个项目中解决了这个问题,实际上相当简单。
定义一个新表
在
tables.py
中,定义一个两列表格。让我们将第一列称为name
,将第二列称为value
:在您的视图中,将数据绑定到表
然后,在您的视图中,您需要从您的视图中提取实例(行)数据库,并使用字典填充表:
然后配置表,放入上下文中并像往常一样发送到模板。您将有一个包含两列的表,显示数据库一行中的所有数据。
I solved this for a project today, it was actually fairly simple.
Define a new table
Within
tables.py
, define a two-column table. Let's call the first columnname
and second columnvalue
:In your view, bind data to table
Then, in your view, you need to pull the instance (row) from your database, and populate the table using a dictionary:
Then configure the table, put in context and send to template as usual. You will have a table with two columns showing all the data from a row of your database.
将 /lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py 中的 result_list(cl) 函数更改为:
Change the result_list(cl) function in /lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py to this: