The documentation on the django newforms library is rather incomplete at the moment. Any functionality outside of the documentation has to be gleaned from the regression test script, the django source, and google.
I got frustrated that every CharField form element would be rendered on the page with exactly the same size, regardless of what max_length I gave them. I didn’t want to have to render each element by hand, so I looked for and found what I was after. Every newforms widget can be “instantiated” (is instantiated even a concept in python?) with a dictionary of attributes which map to html attributes when rendered.
So for a CharField, I would create my forms subclass like so:
from django import newforms as forms
from django.newforms import widgets
class TestForm(forms.Form):
subject = forms.CharField(label="Subject",
max_length=45,
widget=widgets.TextInput({'size': 45, 'bogus': 500}),
required=True)
Rendering this form gives the following output:
<input bogus="500" name="subject" maxlength="45" type="text" id="id_subject" size="45" />
Note the “bogus” attribute. This was to test whether django would simply render any attribute in the dictionary, regardless of validity.
Thanks for blogging this. I was going nuts trying to figure out how to set the size attribute on a FileInput field.