newforms: better table markup

1 view
Skip to first unread message

James Bennett

unread,
Apr 2, 2007, 12:50:43 AM4/2/07
to django-d...@googlegroups.com
At the moment, BaseForm.as_table() does a fairly good job of
presenting the form cleanly, but I think there are a couple things
worth tweaking.

Consider a simple example form:

class SampleForm(forms.Form):
name = forms.CharField()

Calling as_table() will print out an instance of this form like so:

<tr><th><label for="id_name">Name:</label></th><td><input type="text"
name="name" id="id_name" /></td></tr>

This is pretty good as table-based forms go, and passes the biggest
test for form markup in that it associates the input with the label
correctly by using the "for" attribute. But it could be better. For
example (whitespace added to make it easier to read):

<tr>
<th id="header_id_name"><label for="id_name">Name:</label></th>
<td headers="header_id_name"><input type="text" name="name"
id="id_name" /></td>
</tr>

The "headers" attribute is definitely one of the more obscure HTML
attributes, but it does serve a useful purpose (and WCAG 1.0
recommends its use) in allowing a table cell to be associated with the
specific header cells it "belongs" to.

Another possible improvement would be -- since we're outputting XHTML
here -- to also include the wrapping <tbody></tbody> around the
output, since the 'tbody' element is not implied in XHTML (though
we're not providing the wrapping <table></table>, or the wrapping
<ul></ul> in as_list(), which seems weird).

Anyone have strong opinions against this before I whip up a patch?

--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Jacob Kaplan-Moss

unread,
Apr 2, 2007, 9:18:11 AM4/2/07
to django-d...@googlegroups.com
On 4/1/07, James Bennett <ubern...@gmail.com> wrote:
> Anyone have strong opinions against this before I whip up a patch?

+1 on adding "headers", -1 on adding <tbody> (for the same reason we
leave <table> out).

Jacob

Ivan Sagalaev

unread,
Apr 2, 2007, 2:58:31 PM4/2/07
to django-d...@googlegroups.com
James Bennett wrote:
> Another possible improvement would be -- since we're outputting XHTML
> here -- to also include the wrapping <tbody></tbody> around the
> output, since the 'tbody' element is not implied in XHTML (though
> we're not providing the wrapping <table></table>, or the wrapping
> <ul></ul> in as_list(), which seems weird).

Not providing top level element is useful. It allows user to set
whatever attributes on it at a small cost of typing one tag.

Adrian Holovaty

unread,
Apr 2, 2007, 3:31:10 PM4/2/07
to django-d...@googlegroups.com

-1 on <tbody> for the same reason.

As for "headers," I'm +0.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Jason Davies

unread,
Apr 4, 2007, 12:16:05 PM4/4/07
to Django developers
On Apr 2, 8:31 pm, "Adrian Holovaty" <holov...@gmail.com> wrote:

> On 4/2/07, Jacob Kaplan-Moss <jacob.kaplanm...@gmail.com> wrote:


>
> > On 4/1/07, James Bennett <ubernost...@gmail.com> wrote:
> > > Anyone have strong opinions against this before I whip up a patch?
>
> > +1 on adding "headers", -1 on adding <tbody> (for the same reason we
> > leave <table> out).
>
> -1 on <tbody> for the same reason.
>
> As for "headers," I'm +0.

I'm -0 on the "headers" idea, as it makes the HTML bulkier. On the
other hand I guess WCAG compliance is a good thing.

Jason

Reply all
Reply to author
Forward
0 new messages