Everything can be changed. Look under auto_id in the
newforms docs on
djangoproject.com. The default does it exactly the way you
seem to
be, prepending 'id_' to the field name. If you want to set
a class,
you need to change the attrs dict on the field's widget.
All a newforms field really consists of is a field class
that handles
validation, and a widget class that handles making the html
form
element for that field. By defining your form class they
start out in
FormClassName.base_fields['fieldname']. Once you actually
make a form
instance by doing form=FormClassName(), you access them via
form.fields['fieldname'] and not base_fields (because the
Form class
is a metaclass). So, if you have a form instance:
form.fields is a dict of all the form fields.
form['fieldname'] is
the field class instance for the type you gave fieldname
when you
defined the form. form['fieldname'].widget is the widget
class
instance which you either specified, or django supplied the
default
widget for the field type. The widget has an attribute dict
called
'attrs', that is home for the form element attributes like
'class', or
'size', or 'style'. If you put them in the attrs dict, they
show up
on the html element when it's rendered in the template.
Three different ways of doing the exact same thing (adding a
css class
to a field):
You can do in the form class when you define the form,
contract_from =
forms.DateField(widget=TextInput(attrs={'class':'cssclassnam
e'}))
or
After the form as been defined, but not instantiated by
modifying
base_fields
NewEmployeeForm.base_fields['contract_from'].widget.attrs['c
lass'] =
'cssclassname'
or
After the form has been instantiated by modifiying for forms
local
field definitions, 'fields'
form.fields['contract_from'].widget.attrs['class'] =
'cssclassname'
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|