Controlling html display of Charfield length on forms...(newforms?)

1,963 views
Skip to first unread message

NoviceSortOf

unread,
Mar 26, 2009, 5:26:43 AM3/26/09
to Django users

It would seem natural that there would be a length or size parameter
on something in forms and/or models Charfield to control the html
display of the field length.

ie.
firstname = forms.CharField(max_length=20, size = 4)

I've found this hack discussed to the fields.py [link below] but it
dates back a few years and I was curious if there is any update on how
the display of html input field's is now handled by Django.

http://groups.google.com/group/django-users/browse_frm/thread/4f6e2e0c936dcca5/cea43c5582dda436?hl=en&lnk=gst&q=fields.py+length#cea43c5582dda436

Ayaz Ahmed Khan

unread,
Mar 26, 2009, 6:50:30 AM3/26/09
to django...@googlegroups.com

On 26-Mar-09, at 2:26 PM, NoviceSortOf wrote:

> It would seem natural that there would be a length or size parameter
> on something in forms and/or models Charfield to control the html
> display of the field length.


How a field in a form in Django is rendered is determined by the
corresponding widget used for that field. To that end, Django offers a
great deal of flexibility when it comes to customising both the look
and functionality of form fields. Having said that, I should point out
without sounding condescending that one can specify and configure
different supported attributes of an HTML form element through the use
of the `attrs` attribute supported by Django widgets.

For example, to restrict the size of and specify a class for an input
field, the following can be used:

text = forms.CharField(label="text", max_length=10,
widget=forms.TextInput(
attrs={'size':'10', 'class':'inputText'}))

One can toss in all manners of *supported* HTML attributes in there.

--
Ayaz Ahmed Khan

An evil mind is a great comfort.

NoviceSortOf

unread,
Apr 2, 2009, 7:46:11 AM4/2/09
to Django users

text = forms.CharField(label="text", max_length=10,
widget=forms.TextInput(
attrs={'size':'10', 'class':'inputText'}))

Thanks Ayaz the above works perfectly.
Reply all
Reply to author
Forward
0 new messages