'Django ImportError: cannot import name 'ModelForm'
I am pretty much new for Django and trying to create fields to add a new movie to the existing model. It shows an error : ImportError: cannot import name 'ModelForm'
#forms.py file:
from django import forms
from django import ModelForm
from .models import Movie
class MovieForm(ModelForm):
class Meta:
model = Movie
fields = ['genre', 'price', 'rent_time', 'flag']
class CustForm(forms.Form):
f_name = forms.CharField(label='First_Name', max_length=50)
l_name = forms.CharField(label='Last_Name', max_length=50)
address = forms.CharField(label='Address', max_length=125)
cell = forms.IntegerField(label='Cell')
#------------------------------------------------------------------------
#models.py file:
from django.db import models
from django.forms import ModelForm
class Customer(models.Model):
f_name = models.CharField(max_length=50)
l_name = models.CharField(max_length=50)
address = models.CharField(max_length=125)
cell = models.IntegerField(blank=True, null=True)
class Movie(models.Model):
genre = models.CharField(max_length=50)
price = models.FloatField(blank=True, null=True)
rent_time = models.DateTimeField(auto_now_add=True, blank=True)
flag = models.BooleanField(default=True)
user_id = models.ForeignKey('Customer', on_delete=models.SET_NULL,
null=True)
#------------------------------------------------------------------------
#views.py file:
from django.shortcuts import render
from .models import Customer, Movie
# from django.http import HttpResponseRedirect
from .forms import MovieForm, CustForm
def dashboard(request):
customer_data = Customer.objects.all()
context = {'Customer_List': customer_data}
return render(request, 'movie_renting_app/home.html', context)
def movie_list(request):
movie_data = Movie.objects.all()
context = {"Movie_List": movie_data}
return render(request, 'movie_renting_app/all_movies.html', context)
def rented_movies(request):
rented_movie = Movie.objects.filter(flag=True)
context = {"Rented_Movies_List": rented_movie}
return render(request, 'movie_renting_app/rent.html', context)
def add_movies(request):
if request.POST:
form = MovieForm()
print(request)
# return movie_list(request)
else:
form = MovieForm()
return render(request, 'movie_renting_app/new_movie.html',
{'form': form})
def add_customer(request):
if request.POST:
form = request.POST
print(request)
c = Customer(f_name=form['f_name'], l_name=form['l_name'],
address=form['address'], cell=form['cell'])
c.save()
return dashboard(request)
else:
form = CustForm()
return render(request, 'movie_renting_app/new_customer.html',
{'form': form})
def update_customer_info(request):
pass
def available_list(request):
pass
I am getting an error on command prompt. I have no clue what is going wrong with my code. I have tried various ways to import the ModelForm but it showing me the same error message. I am using Django 2.0 and Python 3.6
Solution 1:[1]
The error is probably your second line in forms.py. ModelForm is in django.forms, and can't be imported from django. Try this instead:
from django.forms import ModelForm
Or, change class MovieForm(ModelForm): to class MovieForm(forms.ModelForm): and remove your ModelForm import line.
Solution 2:[2]
ModelForm class is located in Django forms so you can import it as shown below.
from django.forms import ModelForm
or use it directly when subclassing your form class
class CustomForm(forms.ModelForm):
# your form fields here
pass
for more information about ModelForms see django docs
Solution 3:[3]
Replace this wrong code in line 2, "forms.py":
from django import ModelForm # Line 2, "forms.py"
With this correct code:
from django.forms import ModelForm # Line 2, "forms.py"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | davegaeddert |
| Solution 2 | |
| Solution 3 | Kai - Kazuya Ito |

