python - 如何更改 view.py 中 django-tables2 表的列标题?

标签 python django view header django-tables2

我在 Django 项目中使用 django-tables2,我想根据数据库查询动态更改某些列的标题,这是在 view.py 中完成的。

我知道在tables.py中可以更改每一列的“verbose_name”属性,但我想分配一个模板变量“{{ headerxy }}”,以便它动态变化。

或者有没有办法更改 view.py 中的“verbose_name”属性?

类似于:

table.columns['column1'].header = some_data

谢谢

最佳答案

这里您要做的就是在初始化 Table 类时将列名作为参数传递,并在该类的 __init__ 范围内使用它。例如:

表类:

class SomeTable(tables.Table):
    def __init__(self, *args, c1_name="",**kwargs):  #will get the c1_name from where the the class will be called.
        super().__init__(*args, **kwargs)
        self.base_columns['column1'].verbose_name = c1_name
    class Meta:
        model = SomeModel
        fields = ('column1')

查看:

class SomeView(View):
  def get(self, request):
    context = {
      'table': SomeTable(SomeModel.objects.all(), c1_name='some name')
    }
    return render(request, 'table.html', {'context':context})

关于python - 如何更改 view.py 中 django-tables2 表的列标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28088655/

相关文章:

Oracle:创建具有自动增量 id 列的 View

Swift - 将特定的选项卡 Controller 大小传递给另一个 View Controller ?

python - Ctrl-C 从 python 到 bash

python - Matplotlib 和 Pyplot.close() 不释放内存? - 后端相关的 Qt4Agg

python - 聚合方法和 lambda 函数

ajax - IE 想从 Django 下载返回的 JSON

python - 在 Django 模板中呈现标签名称而不是整数字段

python - 如果用户在Python中输入相同的字符串两次,如何打印特定的消息?

python - 是否可以直接从 S3 打开文件

jquery - Django AJAX - 建议