iscream
iscream

Reputation: 790

How to customize Django ModelForm fields?

Hi I am trying to customize my ModelForm in Django. The form looks like this:

class RegistrationForm(forms.ModelForm):
    class Meta:
        model = Registration
        fields = ['name', 'age', 'details'......]

I am trying to add different classes to different fields, along with placeholders and no labels. How do I do that? Is there a way to carry this out in the templates?

Upvotes: 0

Views: 473

Answers (1)

Gilbish Kosma
Gilbish Kosma

Reputation: 887

You can add class and other parameters to a field using widget

   class RegistrationForm(forms.ModelForm):
      name = forms.CharField(label=' ',widget=forms.TextInput(attrs={'class':'textClass','placeholder':'Enter Name'}))
      details = forms.CharField(widget=forms.Textarea(attrs={'class':'special','maxlength':'100'}))
      class Meta:
         model = Registration
         fields = ['name', 'age', 'details'......]

For brief explanation: Documentation

Upvotes: 2

Related Questions