2017-09-13 6 views
0

extra_colums를 사용하려고 시도했지만 오류가 발생하지 않지만 테이블이 표시되지 않습니다. here의 설명서를 사용했습니다. 테이블에 체크 박스가있는 열을 추가하려고합니다. 이미 테이블을 미리 정의하고 일부 필드를 제외 할 수 있지만 문서를 사용하면 새 열을 추가하는 방법을 알 수 없습니다. 내가 뭔가를 놓치고 있어야합니다django_tables2.tables에서 extra_columns 사용.

구현은 아래에서 볼 수 있습니다. 어떤 도움을 주시면 감사하겠습니다. 보기

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

IN

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',) 

TABLES.PY

감사

당신은 테이블 생성자에 인수로 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로 추가 허용, 그래서 이것은 트릭 수행해야합니다 문서 테이블 생성자에 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

참고, 그래서 그것을 조금 업데이트 : http://django-tables2.readthedocs.io/en/latest/pages/reference.html – Jieter