[Fixed]-Django – Specify width of django.db.models.(???)Field forms

1👍

you can set it a few ways…

you can do it without having to define the widget and just set the html ‘style’ property on the element

Option 1

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyFormForm, self).__init__(*args, **kwargs)
        self.fields['title'].widget.attrs['style'] = "width:200px"

Option 2

class MyForm(forms.ModelForm):
    class Meta:
        model = Foo
        'widgets' = {
            'title' = forms.CharField(attrs={'size':'80'}),
        }

Option 3

   class MyForm(forms.ModelForm):
        title = models.CharField(max_length=200, widget=forms.CharField(attrs={'size':'80'})

Leave a comment