在 django_tables2.tables.Table 中使用 extra_columns

Using extra_columns in django_tables2.tables.Table

尝试使用 extra_colums 并且没有出现错误,但是 table 没有出现。我使用了 here 中的文档。我正在尝试在 table 中添加一个包含复选框的列。我已经预定义了 table 并且可以排除一些字段但是使用文档我无法弄清楚如何添加新列。我一定是漏了什么

实现如下所示。将不胜感激任何帮助。谢谢

在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

您正在 table 实例上设置属性,而不是将 extra_columns 作为参数传递给 table 构造函数。使用 extra_columns 应该是这个样子:

class MyTable(tables.Table):
    name = tables.Column()

table = MyTable(data, order_by='-country', extra_columns=[
    ('country', tables.Column(verbose_name=_('country')))
])

在您的例子中,使用基于 class 的视图,table 是在视图的 get_table 方法中创建的。 get_table_kwargs 方法允许将 kwargs 添加到 table 创建调用中,所以这应该可以解决问题:

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_table_kwargs(self):
       return {
           'extra_columns': (('Change', CheckBoxColumn(checked=False)),),
           'exclude': ('project_status', 'department')
       }