python-django-tables2编辑所有BooleanColumn的yesno参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-django-tables2编辑所有BooleanColumn的yesno参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1715字,纯文字阅读大概需要3分钟。
内容图文
![python-django-tables2编辑所有BooleanColumn的yesno参数](/upload/InfoBanner/zyjiaocheng/680/e3bf5006f82943e4b67c058b67e05bc2.jpg)
我有一个table.py,我想在其中更改每个BooleanColumn的True和False值的图标.我知道可以通过BooleanColumn的yesno参数对其进行修改,但是我不知道如何覆盖所有BooleanColumns的默认值.这是table.py的代码(aacsb,amba,equis,mba,bsc,msc和doubedegree是BooleanFields):
from django_tables2 import Column, Table
from manager.models import Partner
class PartnerTable(Table):
country_name = Column(accessor='country.name', verbose_name='Country')
region_name = Column(accessor='country.region.name', verbose_name='Region')
class Meta:
model = Partner
fields = ('name',
'country_name',
'region_name',
'website',
'aacsb',
'amba',
'equis',
'mba',
'bsc',
'msc',
'doubledegree',
)
解决方法:
1)因此,您可以简单地覆盖yesno,其默认值为“?,?”(只是str):
some_name = BooleanColumn(yesno='1,2')
或删除文字:
some_name = BooleanColumn(yesno=',')
2)使用CSS您可以指定自定义图片(不要忘记设置yesno =’,’):
span.true {
background: url(../img/true.gif) top center no-repeat;
}
span.false {
background: url(../img/false.gif) top center no-repeat;
}
3)指定一些额外的属性以覆盖(但不要指定class!):
some_name = BooleanColumn(attrs={'span': {'style': 'color:blue'}})
4)如果出于某些原因要更改默认的类设置行为(对或错),则应重写BooleanColumn及其方法render
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django_tables2.utils import AttributeDict
class CustomBooleanColumn(BooleanColumn):
def render(self, value):
value = bool(value)
text = self.yesno[int(not value)]
html = '<span %s>%s</span>'
class_name = 'some_class_false'
if value:
class_name = 'some_class_true'
attrs = {'class': 'class_name'}
attrs.update(self.attrs.get('span', {}))
return mark_safe(html % (AttributeDict(attrs).as_html(), escape(text)))
并覆盖您的领域
some_name = CustomBooleanColumn(yesno=',')
内容总结
以上是互联网集市为您收集整理的python-django-tables2编辑所有BooleanColumn的yesno参数全部内容,希望文章能够帮你解决python-django-tables2编辑所有BooleanColumn的yesno参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。