2017-09-13 6 views
0

extra_columsを使用しようとしてもエラーは発生しませんが、テーブルは表示されません。私はhereのドキュメントを使用しました。私はテーブルにチェックボックスを持つ列を追加しようとしています。私は既にテーブルをあらかじめ定義していて、いくつかのフィールドを除外することができますが、ドキュメントを使って新しいカラムを追加する方法を理解することはできません。私は何かが欠けている必要がありますdjango_tables2.tables.Tableでextra_columnsを使用する

実装は以下で見ることができます。助けていただければ幸いです。 VIEW

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を渡していない、テーブルのインスタンスの属性を設定しています。テーブルはあなたのビューのget_table方法で作成され、クラスベースのビューを使用して、あなたの場合は

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

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

:extra_columnsを使用すると、次のようになります。 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

関連する問題