* Joe Reitman <jreitma...@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
-- 
Tim Johnson
http://www.tj49.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20190512190206.GE2372%40mail.akwebsoft.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to