0
試圖使用extra_colums並且不會出現錯誤,但表不顯示。我使用了here的文檔。我正在嘗試添加一個將複選框放入表格的列。我已經預定義了表格,可以排除一些字段,但使用文檔我無法弄清楚如何添加新列。我必須缺少一些東西在django_tables2.tables.Table中使用extra_columns
實現可以在下面看到。將不勝感激任何援助。由於
IN TABLES.PY
import django_tables2 as tables
from project.models import Project
class ProjectTable(tables.Table):
project_name = tables.TemplateColumn("""
{%if record.department == "TRACER"%}
<a href=" {% url 'project_detail' record.id %}">
{{ value }}
</a>
{%else%}
{{value}}
{%endif%}
""", orderable=True, accessor='name', verbose_name="Project
name")
project_type = tables.TemplateColumn("""
{% if value == 'Yes' %}Special{% else %}Normal{% endif %}
""", orderable=True, accessor='is_special',
verbose_name="Project type")
project_code = tables.Column(accessor='code', verbose_name="Project
code")
project_status = tables.Column(accessor='status',
verbose_name="Project status")
department = tables.Column(accessor='department',
verbose_name="Department")
class Meta:
model = Project
attrs = {'class': 'table table-striped table-hover'}
sequence = (
'project_name', 'project_type', 'project_code',
'project_status',)
視圖
from project.tables import ProjectTable
from django_tables2.columns import CheckBoxColumn
class AllocationChangeView(PagedFilteredTableView):
display_message = "You need to be logged in to view this page"
table_class = ProjectTable
queryset = Project.objects.all()
template_name = 'matter_allocation/change_project.html'
paginate_by = ITEMS_PER_PAGE
formhelper_class = ProjectFilterFormHelper
filter_class = ProjectFilter
def get_context_data(self, **kwargs):
context = super(AllocationChangeView,
self).get_context_data(**kwargs)
table = context['table']
table.exclude = ('project_status','department')
table.extra_columns =(('Change',CheckBoxColumn(checked=False)),)
context['table'] = table
return context
注意的是,文件是缺乏關於如何kwargs傳遞到表構造了一點,所以我更新了這一點: http://django-tables2.readthedocs.io/en/latest/pages/reference.html – Jieter