* Joe Reitman <
jreit...@gmail.com> [190512 10:31]:
> Tim,
>
> Here is an example of a custom form field limiting the input to 100
> characters. The model is defined to accept 255 chars. BTW, the text widget
> 'attrs' sets the HTML form element attributes.
>
> class SearchForm(forms.Form):
>
> search_for = forms.CharField(
> label='',
> label_suffix='',
> max_length=100,
> required=True,
> widget=forms.TextInput(attrs={'placeholder': 'search', ' autofocus': ''}),
> help_text='',
> error_messages={'required': ''},
> )
Hi Joe:
Thanks for the reply.
I did try using a custom class in admin.py
class LongTextinput(TextInput):
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
# code below aims to add a size="100" attribute
# to rendered html
attrs.setdefault('size', 100)
super(LongTextinput, self).__init__(*args, **kwargs)
implemented with formfields_ovverides as
in :
formfield_overrides = {
models.CharField: {'widget': LongTextinput},
}
but did not have the desired effect.
the larger question is:
why is formfield_overrides not having the effect I expected?
I.E. size="100" is added to the rendered html.
According to documentation the implementation should be pretty straightforward.
Do you suppose that there is a particular plugin that enables formfield_overrides?
**OR** is {{ form.as_p }} in the template inhibiting the rendering?
My goal was not to change the max_length attribute, but the size attribute in
the rendered HTML code.
For the record, css widens the input field, and that gave me the
input width I sought, but the edification I seek is to understand
how to use formfield_overrides.
cheers