Reputation: 71
Some columns in my django table happened to be empty and as a result the text rendered there is 'None'. I would like to see a blank space instead.
django tables2 has some documentation on the subject, but I don't understand it completely. Where do I have to define this empty_text behavior parameter? Tried in the relevant class meta, but apparently it has no effect.
Upvotes: 4
Views: 4144
Reputation: 5867
You can override the default value in your column definition.
If you are not explicitly declaring your column (for example you are letting tables2 figure it out from your model) then you will have to define it so that you can set options for it. It is ok to do that with data coming from models.. as long as the column name you define matches the model field name it will match them up.
class TableClass(tables.Table):
mycolumn = tables.Column(default=' ')
If you need to dynamically work out your default value on a row-by-row basis then define your column and pass empty_values=[], eg:
class TableClass(tables.Table):
mycolumn = tables.Column(empty_values=[])
This tells tables2 that it should not consider any values to be 'empty'.. you can then declare a custom rendering method for that column:
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
Keep in mind that render_
methods are not called if tables2 thinks the value is blank hence you must also set empty_values=[]
.
Here is the tables2 documentation which describes how custom render methods work: http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html?highlight=empty#table-render-foo-method
Upvotes: 10