views:

375

answers:

4

I am writing out a table using just the template system in Django, and it is not showing the border around empty table cells.

No problem I thought - I've solved this problem before. I put &nbsp in any cell that was to be left empty. Django kindly converted the ampersand to &amp so that I have &ampnbsp in the empty cells, and it shows the &nbsp when viewed in the browser.

I googled it, and tried putting {%autoescape off%} and {%endautoescape%} around the table in question, but it didn't do any good either.

I also tried adding autoescape=False to the context constructor, but that didn't help either.

What's the magic trick to make Django show the border around empty cells?

+1  A: 

There is the empty cells CSS property.

http://www.w3.org/TR/CSS21/tables.html#empty-cells

I can't remember if it works in all browsers or not though

benlumley
+1  A: 

This is a known CSS/HTML 'problem.' You want to use Django's "default" filter.

{{value|default:" "}}

(I'll be damned if I could get that to come out right. In SO, how do you write "nbsp;" without the & in front causing everything to disappear and be replaced by a blank?)

Peter Rowell
I used " ", which seemed to work okay.
Justin Voss
Hmm, that didn't work either. Odd, wonder if it's the quotes. Sorry about the edit pollution.
Carl Meyer
+1  A: 

Django does in no way control the appearance of your table. Tinkering with autoescape is also superfluous and could turn out dangerous.

Are you using CSS for styling the table? By using a property like e.g.

td {
  border: 1px solid red;
}

you can make each cell having a red border. No matter if it's empty or not.

webjunkie
I tried this - it did not work. The empty cells didn't get a border. I'm using internet explorer 7.
Curt
You are somehow using tables wrong then. Unless a cell has zero width and height, it will show a border if it has one. If you have cells with content in the same row or column, the cell in question does automatically have a width.
webjunkie
A: 

What you really need to do is get a non-breaking space in those empty cells, and prevent Django from escaping the HTML entity. Could you chain a few filters together to achieve what you're looking for?

{{ value|default:" "|safe }}

Edit: I should mention that   is the same as  , is just doesn't get mangled by the SO parser.

Justin Voss