2017-09-13 56 views
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 

回答

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') 
     } 
+0

注意的是,文件是缺乏關於如何kwargs傳遞到表構造了一點,所以我更新了這一點: http://django-tables2.readthedocs.io/en/latest/pages/reference.html – Jieter