Improvement for newforms CharField - accepting size HTML attribute
|Reported by:||danh@…||Owned by:||nobody|
|Severity:||Keywords:||newforms input size|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The newforms.fields.CharField doesn't accept HTML size attribute and as a result during rendering all fields get the same length.
This is also the cause for #5609.
It would be very easy to modify it to accept it. Right now I am subclassing CharField to add this functionality but it would be more convenient to have it in the framework.
This is what I'm using right now:
import django.newforms as forms class CharField(forms.fields.CharField): def __init__(self, size=None, *args, **kwargs): self.size = size super(CharField, self).__init__(*args, **kwargs) def widget_attrs(self, widget): attrs = super(forms.fields.CharField, self).widget_attrs(widget) if self.max_length is not None and isinstance(widget, (forms.fields.TextInput, forms.fields.PasswordInput)): attrs['size'] = str(self.size) return attrs