Django ImportError:无法导入名称' ModelForm'

时间:2018-06-05 19:54:01

标签: django python-3.x django-models django-forms django-views

I have fixed the error from given question and now I am getting an error on command promp我是Django的新手,并尝试创建字段以向现有模型添加新电影。它显示错误:ImportError:无法导入名称' 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

我在命令提示符下遇到错误。 我不知道我的代码出了什么问题。我已经尝试了各种方法来导入ModelForm,但它向我显示了相同的错误消息。我正在使用Django 2.0和Python 3.6

2 个答案:

答案 0 :(得分:2)

ModelForm类位于Django表单中 所以你可以导入它,如下所示。

from django.forms import ModelForm

或在子类化表单类时直接使用它

class CustomForm(forms.ModelForm):
    # your form fields here
    pass

有关ModelForms的更多信息,请参阅django docs

答案 1 :(得分:1)

错误可能是你在forms.py中的第二行。 ModelForm位于django.forms,无法从django导入。试试这个:

from django.forms import ModelForm

或者,将class MovieForm(ModelForm):更改为class MovieForm(forms.ModelForm):并删除ModelForm导入行。