django-tables2每行添加按钮 [英] django-tables2 add button per row
问题描述
我正在使用django-tables 2渲染一个查询集,但是由于该表是一次渲染的,所以我无法管理以下内容:
首先,我应该提到的是,表的行数与每个查询集,所以我事先不知道它们的确切数量。我需要的是每行一个按钮,将检索到的对象加载到表单的字段中。
我使用默认方式渲染表:
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?
推荐答案
您可以在表格中创建模板列,它将呈现一些东西,例如一个按钮:
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
在模板my_column中,该行位于变量记录中:
In the template my_column the row is in the variable record:
{{ record.my_field }}
这篇关于django-tables2每行添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!