Cavin blake
Cavin blake

Reputation: 65

Django widget in forms is not working correctly

I'm using django widgets into my forms.py for the content field. But whenever I change the column and rows in forms.py with the widgets it's not changing in the template.

My forms.py:

from django import forms
from .models import Comment


class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        content = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Text goes here', 'rows': '4', 'cols': '10'}))
        fields = ('content',)

Upvotes: 1

Views: 3011

Answers (1)

bug
bug

Reputation: 4130

Fields must be defined outside of Meta:

class CommentForm(forms.ModelForm):
    content = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Text goes here', 'rows': '4', 'cols': '10'}))

    class Meta:
        model = Comment
        fields = ('content',)

Also, if you only want to customize the widget you can use the widgets meta:

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('content',)
        widgets = {
            'content': forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Text goes here', 'rows': '4', 'cols': '10'})
        }

Check the overriding the default fields section of the Django documentation for further details.

Upvotes: 2

Related Questions