作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 django 表中的某些列碰巧是空的,因此那里呈现的文本是“无”。我希望看到一个空白区域。
django tables2 有 some documentation关于这个问题,但我不完全理解。我必须在哪里定义这个 empty_text 行为参数?在相关类元中尝试过,但显然没有效果。
最佳答案
您可以覆盖列定义中的默认值。
如果您没有明确声明您的列(例如,您让 tables2 从您的模型中找出它),那么您将必须定义它,以便您可以为其设置选项。可以对来自模型的数据执行此操作。只要您定义的列名称与模型字段名称匹配,它就会匹配它们。
class TableClass(tables.Table):
mycolumn = tables.Column(default=' ')
如果您需要逐行动态计算默认值,请定义您的列并传递 empty_values=[],例如:
class TableClass(tables.Table):
mycolumn = tables.Column(empty_values=[])
这告诉 table2 它不应该将任何值视为“空”。然后您可以为该列声明自定义呈现方法:
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
关于django-tables2 - 为 django-tables2 设置空文本行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24989473/
我是一名优秀的程序员,十分优秀!