在 django_tables2 中转置 Django 表

发布于 2024-11-27 19:56:49 字数 331 浏览 3 评论 0原文

我正在尝试创建一个视图,该视图本质上显示有关此类记录的信息

|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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷又爱笑 2024-12-04 19:56:49

您需要编写自己的模板,通过迭代表的属性并编写正确的标签来生成所需的 HTML。用于说明如何执行此操作的一个很好的示例是用于 as_html() 函数的模板:https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html

You 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

我最亲爱的 2024-12-04 19:56:49

我今天在一个项目中解决了这个问题,实际上相当简单。

定义一个新表

tables.py 中,定义一个两列表格。让我们将第一列称为 name ,将第二列称为 value

class RowTable(tables.Table):
    name = tables.Column()
    value = tables.Column()

    class Meta:
        template = 'django_tables2/bootstrap.html'
        attrs = {'class': 'table table-striped table-responsive'}  

在您的视图中,将数据绑定到表

然后,在您的视图中,您需要从您的视图中提取实例(行)数据库,并使用字典填充表:

row_instance = Model.objects.get(foo=foo.id)
#Extract column data from instance be sure it is string!
height = str(row_instance.height)
weight = str(row_instance.weight)
nationality = row_instance.nationality

#Create dictionary of data in form table will recognize
row_data =[ {'name': 'height', 'value': height},
            {'name': 'weight', 'value': weight},
            {'name': 'nationality', 'value': nationality} ]

#Bind to table and configure
table = RowTable(row_data)

然后配置表,放入上下文中并像往常一样发送到模板。您将有一个包含两列的表,显示数据库一行中的所有数据。

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 column name and second column value:

class RowTable(tables.Table):
    name = tables.Column()
    value = tables.Column()

    class Meta:
        template = 'django_tables2/bootstrap.html'
        attrs = {'class': 'table table-striped table-responsive'}  

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:

row_instance = Model.objects.get(foo=foo.id)
#Extract column data from instance be sure it is string!
height = str(row_instance.height)
weight = str(row_instance.weight)
nationality = row_instance.nationality

#Create dictionary of data in form table will recognize
row_data =[ {'name': 'height', 'value': height},
            {'name': 'weight', 'value': weight},
            {'name': 'nationality', 'value': nationality} ]

#Bind to table and configure
table = RowTable(row_data)

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.

江湖彼岸 2024-12-04 19:56:49

将 /lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py 中的 result_list(cl) 函数更改为:

def result_list(cl):
 """
 Displays the headers and data list together
 """
 headers = list(result_headers(cl))
 num_sorted_fields = 0
 for h in headers:
     if h['sortable'] and h['sorted']:
         num_sorted_fields += 1
 result1 = list(results(cl))
 result2 = map(list, zip(*result1))
 return {'cl': cl,
         'result_hidden_fields': list(result_hidden_fields(cl)),
         'result_headers': headers,
         'num_sorted_fields': num_sorted_fields,
         'results': result2}

Change the result_list(cl) function in /lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py to this:

def result_list(cl):
 """
 Displays the headers and data list together
 """
 headers = list(result_headers(cl))
 num_sorted_fields = 0
 for h in headers:
     if h['sortable'] and h['sorted']:
         num_sorted_fields += 1
 result1 = list(results(cl))
 result2 = map(list, zip(*result1))
 return {'cl': cl,
         'result_hidden_fields': list(result_hidden_fields(cl)),
         'result_headers': headers,
         'num_sorted_fields': num_sorted_fields,
         'results': result2}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文