【发布时间】: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