django-tables2 add button per row

烈酒焚心 提交于 2019-12-09 16:55:29

问题


I'm rendering a queryset with django-tables 2 but since the table is rendered at once I can't manage the following: Firstly, I should mention that the number of rows of the table is different with each queryset so I don't know the exact number of them in advance. What I need, is to have one button per row that loads the retrieved object inside the fields of a form. I render the table with the default way:

{% load render_table from django_tables2 %}
{% render_table table %} 

When I try to iterate over the rows of the tables I get the error 'table is not iterable'. So how can I add one button per row?


回答1:


You can create in your table a template column, this will render something, a button for example:

class MyTables(tables.Table):
  ...
  my_column = tables.TemplateColumn(verbose_name=_('My Column'),
                                    template_name='app/my_column.html',
                                    orderable=False) # orderable not sortable

In the template my_column the row is in the variable record:

{{ record.my_field }}


来源:https://stackoverflow.com/questions/12192004/django-tables2-add-button-per-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!