在django_tables2.tables.Table

时间:2017-09-13 09:46:41

标签: django django-views django-tables2

尝试使用extra_colums并且没有错误,但表格没有显示。我使用了here中的文档。我正在尝试添加一个列,该列将具有复选框到表中。我已经预定义了表并且可以排除某些字段但是使用文档我无法弄清楚如何添加新列。我一定错过了什么

实施情况见下文。非常感谢任何帮助。感谢

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

1 个答案:

答案 0 :(得分:1)

您正在为表实例设置属性,而不是将extra_columns作为参数传递给表构造函数。使用extra_columns看起来应该是这样的:

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

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

在您的情况下,使用基于类的视图,该表是在视图的get_table方法中创建的。 get_table_kwargs方法允许将kwargs添加到表创建调用中,因此这应该可以解决问题:

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')
       }
相关问题