【问题标题】:Using extra_columns in django_tables2.tables.Table在 django_tables2.tables.Table 中使用 extra_columns
【发布时间】:2018-02-21 23:13:07
【问题描述】:

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

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

在 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

【问题讨论】:

    标签: django django-views django-tables2


    【解决方案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')
           }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2018-01-26
    • 2013-10-24
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    相关资源
    最近更新 更多