4
You’ll have to choose width and or height as limiting factors for your columns. Assuming you can’t dictate the content length, you could choose truncating what is shown.
as shown in
Set the table column width constant regardless of the amount of text in its cells?
and
Using CSS to create table cells of a specific width with no word wrapping
, it is hard or impossible to set the table column width directly without fiddling with the table layout and width settings; alternatively, you could wrap every content in a div, and then apply your formatting to those divs.
To achieve this in tables2, I overrode the table.Column:
class DivWrappedColumn(tables.Column):
def __init__(self, classname=None, *args, **kwargs):
self.classname=classname
super(DivWrappedColumn, self).__init__(*args, **kwargs)
def render(self, value):
return mark_safe("<div class='" + self.classname + "' >" +value+"</div>")
create the column in the table:
custom_column = DivWrappedColumn(classname='custom_column')
and then apply the css:
div.custom_column {
white-space: normal;
width: 200px;
height: 45px;
}
This results in a fixed-width, fixed height cell that wraps until there are no more rows and then truncates.
Alternatively, use “white-space: nowrap”, and omit the height; then the cell is just truncated (but users can scroll).
2
# assume columns c1 and c2 that should be only as wide as needed
# to fit their content
# and a column c3 which takes up the rest of the table width
class MyTable(tables.Table):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.columns['c1'].column.attrs = {"td":{"style" : "width:1%;" }}
self.columns['c2'].column.attrs = {"td":{"style" : "width:1%;" }}
- [Django]-DRF : parameter in custom permission class with function based views
- [Django]-How to allow editing of templates for email by admin user in Django?
- [Django]-Loading Fixtures Django
- [Django]-Why the time is different from my TIME_ZONE in settings.py