将附加参数传递给 django_table2 TemplateColumn

cwh*_*rer 2 django-tables2

在我的 django 项目中,我有很多返回模型的表。最后一列主要是操作列,用户可以在其中编辑或删除实例。如果我想将其他参数传递给 TemplateColumn,如果在某些表中我需要编辑和删除按钮,而在其他表中我只需要编辑和信息按钮,我该如何继续?我想使用相同的 template.html 但其中有条件。这是我在表中的内容:

import django_tables2 as tables
from select_tool.models import DefactoCapability

class DefactoCapabilityTable(tables.Table):

    my_column = tables.TemplateColumn(verbose_name='Actions', template_name='core/actionColumnTable.html')

    class Meta:
         model = DefactoCapability
         template_name = 'django_tables2/bootstrap-responsive.html'
         attrs = {'class': 'table table-xss table-hover'}
         exclude = ( 'body', )
         orderable = False
Run Code Online (Sandbox Code Playgroud)

如何检查操作的权限以便显示或不显示按钮?

Jie*_*ter 5

引用TemplateColumn文档

\n\n
\n

模板对象被创建 [...] 并使用包含以下内容的上下文进行渲染:

\n\n
    \n
  • record\xe2\x80\x93 当前行的数据记录
  • \n
  • value\xe2\x80\x93 记录中与当前列对应的值
  • \n
  • default\xe2\x80\x93 用作后备的适当默认值
  • \n
  • row_counter\xe2\x80\x93 此单元格正在渲染的行号。
  • \n
  • extra_context使用参数传递的任何上下文变量TemplateColumn
  • \n
\n
\n\n

所以你可以这样做:

\n\n
my_column = tables.TemplateColumn(\n    template_name=\'core/actionColumnTable.html\',\n    extra_context={\n        \'edit_button\': True,\n    }\n)\n
Run Code Online (Sandbox Code Playgroud)\n\n

上下文还包含调用模板的完整上下文{% render_table %}。因此,如果您有\'django.template.context_processors.request\'context_processors您可以使用 访问当前用户{{ request.user }}

\n