【发布时间】:2016-04-06 06:18:53
【问题描述】:
我的 django 表中的某些列碰巧是空的,因此呈现在那里的文本是“无”。 我希望看到一个空白区域。
django tables2 在这个主题上有some documentation,但我并不完全理解。 我必须在哪里定义这个 empty_text 行为参数?在相关的类元中试过,但显然没有效果。
【问题讨论】:
标签: django-tables2
我的 django 表中的某些列碰巧是空的,因此呈现在那里的文本是“无”。 我希望看到一个空白区域。
django tables2 在这个主题上有some documentation,但我并不完全理解。 我必须在哪里定义这个 empty_text 行为参数?在相关的类元中试过,但显然没有效果。
【问题讨论】:
标签: django-tables2
您可以覆盖列定义中的默认值。
如果您没有明确声明您的列(例如,您让 tables2 从您的模型中找出它),那么您必须定义它,以便您可以为其设置选项。可以对来自模型的数据执行此操作。只要您定义的列名称与模型字段名称匹配,它就会匹配它们。
class TableClass(tables.Table):
mycolumn = tables.Column(default=' ')
如果您需要逐行动态计算默认值,请定义您的列并传递 empty_values=[],例如:
class TableClass(tables.Table):
mycolumn = tables.Column(empty_values=[])
这告诉tables2它不应将任何值视为“空”。然后您可以为该列声明自定义呈现方法:
def render_mycolumn(value):
# This is a very simplified example, you will have to work out your
# own test for blankness, depending on your data. And, if you are
# returning html, you need to wrap it in mark_safe()
if not value:
return 'Nobody Home'
return value
请记住,如果tables2 认为该值为空白,则不会调用render_ 方法,因此您还必须设置empty_values=[]。
这里是描述自定义渲染方法如何工作的 tables2 文档:http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html?highlight=empty#table-render-foo-method
【讨论】: