Attribute error: module 'django.forms.forms has no attribute 'ModelForm'

Viewed 9652
from django.forms import forms
from .models import StudentModel


class StudentForm(forms.ModelForm):
    name = forms.CharField(label='Name', max_length=200, widget=forms.TextInput(attrs={
      'class': 'form-control',
       'placeholder' : 'Enter Name Here '
    }))

    age = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Age '
    }))

    address = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Address '
    }))

    email = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Enter  Email Here '
    }))

    pin = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Enter Pin Here '
    }))

    mob = forms.CharField(max_length=200, required=False, widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Enter Mobile Number Here '
    }))

    class Meta():
        model = StudentModel
        fields = ['name', 'age', 'address', 'email', 'pin', 'mob']

3 Answers

When I got the "AttributeError: module 'django.forms.forms' has no attribute 'ModelForm' ", I checked my django modules and changed the field below. When I first checked my module it was like this:

from django.forms import forms
    
#then I change as below field:
    
from django import forms

=> forms.ModelForm problem solved

It should be from django.forms import ModelForm then you can use class StudentForm(ModelForm): or from django import forms.

from django.forms import ModelForm

class SampleForm(forms.ModelForm):

class Meta: 
    model = SampleModel
    fields = ['name']

Change the from django.forms import forms to from django.forms import ModelForm and this should work.

Related