Django-tables2:更改列标题中显示的文本 [英] Django-tables2: Change text displayed in column title

查看:56
本文介绍了Django-tables2:更改列标题中显示的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL视图(将View创建为Select ...),并且已成功将视图连接到如下模型:

I am working with a MySQL view (Create View as Select ...) and have successfully manged to connect the view to a model like this:

#models.py
class Dashboard(models.Model):
    devenv = models.CharField(max_length=30, primary_key=True)
    numberofissues = models.BigIntegerField()
    class Meta:
        managed=False
        db_table = 'stability_dashboard'

我还设法使用示例中的样板代码在表格中显示数据:

I have also managed to display data in a table using the boiler plate code from the example:

#tables.py
class DashboardTable(tables.Table):
    class Meta:
        model = Dashboard
        attrs = {'class': 'paleblue'}

#views.py
def dashboard(request):
    table = DashboardTable(Dashboard.objects.all())
    RequestConfig(request).configure(table)
    return render(request, 'uptime/dash.html', {'table': table})

我现在要喜欢将每列中显示的标题更改为更容易理解的内容,例如空格。代替'devenv'=>'开发环境'

I would now like to change the title displayed in each column to something more understandable including spaces e.g. Instead of 'devenv' => 'Development Environment'

推荐答案

只需在表中添加要覆盖其名称的列即可。 py。例如

Just add the columns whose names you want to override in your tables.py. For instance


#tables.py
import django_tables2 as tables
from models import Dashboard

class DashboardTable(tables.Table):
  devenv = tables.Column(verbose_name= 'Development Environment' )

  class Meta:
    model = Dashboard
    attrs = {'class': 'paleblue'}

另一种(可能更多的DRY)解决方案是保留table.py并添加verbose_name在模型定义中:

Another (probably more DRY) solution is to leave tables.py as is and add verbose_name in your model definition:


#models.py
class Dashboard(models.Model):
    devenv = models.CharField(max_length=30, primary_key=True, verbose_name='Development Environment')
    numberofissues = models.BigIntegerField(verbose_name='Number of Issues')
    class Meta:
        managed=False
        db_table = 'stability_dashboard'

这篇关于Django-tables2:更改列标题中显示的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆