Class Attribute in New Forms Input Elements

1 view
Skip to first unread message

Cristian

unread,
Nov 21, 2007, 2:16:16 AM11/21/07
to Django users
Hi,

I've been using the old forms for a while but I'm starting to move
over to the new forms module. One difference that's annoying me is the
lack of the class attribute in the input tags. I use the class tags in
my CSS to change colors and widths. Is there a way to add those back
in (without having to dig into the django code)?

Thanks,
Cristian

James Bennett

unread,
Nov 21, 2007, 2:44:18 AM11/21/07
to django...@googlegroups.com

The 'attrs' argument to the Widget class used with each particular
Field (the Widget is what actually renders the HTML) accepts a
dictionary which will become HTML attribute names and values. For
example:

username = forms.CharField(widget=forms.TextInput(attrs={'class': 'myclass'}))

will become:

<input type="text" class="myclass" />

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

Cristian

unread,
Nov 21, 2007, 3:17:34 PM11/21/07
to Django users
Thanks for the info. Unfortunately, I forgot to mention key point
(absentminded me) that I'm using the form_for_model function to create
many of these Form classes. Since I create many of these Form classes
on the fly, is there another way of specifying input element
attributes?

Thanks

On Nov 20, 11:44 pm, "James Bennett" <ubernost...@gmail.com> wrote:

James Bennett

unread,
Nov 21, 2007, 3:31:09 PM11/21/07
to django...@googlegroups.com
On 11/21/07, Cristian <super.sg...@gmail.com> wrote:
> Thanks for the info. Unfortunately, I forgot to mention key point
> (absentminded me) that I'm using the form_for_model function to create
> many of these Form classes. Since I create many of these Form classes
> on the fly, is there another way of specifying input element
> attributes?

If you want custom behavior from the form, you should probably be
writing a custom form class.

Daniel Roseman

unread,
Nov 21, 2007, 3:39:09 PM11/21/07
to Django users
On Nov 21, 8:17 pm, Cristian <super.sgt.pep...@gmail.com> wrote:
> Thanks for the info. Unfortunately, I forgot to mention key point
> (absentminded me) that I'm using the form_for_model function to create
> many of these Form classes. Since I create many of these Form classes
> on the fly, is there another way of specifying input element
> attributes?
>
> Thanks

You want the formfield_callback parameter to form_for_model. This is
how I do it - admittedly a bit hackish, but works. You can also use it
for other attr parameters, such as field size.

def my_callback(field, **kwargs):
formfield = field.formfield(**kwargs)
t = type(formfield.widget)
formfield.widget = t(attrs={'class':'myclass'})

...

MyForm = forms.form_for_model(Model, formfield_callback=my_callback)

--
DR

Cristian

unread,
Nov 21, 2007, 3:54:45 PM11/21/07
to Django users
On Nov 21, 12:31 pm, "James Bennett" <ubernost...@gmail.com> wrote:

> If you want custom behavior from the form, you should probably be
> writing a custom form class.
>

I don't think this is that custom of a behavior. The old forms had
this behavior by default and this customization doesn't affect
behavior or validation. It seems like a violation of DRY to create a
custom Form class that maps exactly to a Model class only to add
attributes to the html.

I think the old forms default of having class attributes for each
input type (e.g. class=vTextField for type=text) was very helpful for
CSS purposes to help differentiate input elements. Just my 2 cents.
Thanks.
Reply all
Reply to author
Forward
0 new messages