Django ModelForm has no model class specified

我正在尝试使用ModelForm: 

class RegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password',widget=forms.PasswordInput)
    password2 =forms.CharField(label='Confirm Password',widget=forms.PasswordInput)

class Meta:
    model = User
    fields = ("username", "email")

我检查了这个,但是找不到我的错误。当我在浏览器中调用视图时,它会给我以下错误:

ModelForm has no model class specified

我已经测试了在同一个URL上用简单的“foo user”代码调用模型的视图,但是当我尝试这个代码时,我得到了上面的类错误。

4 个解决方案

#1

It should be model instead of Model (and without the trailing `, but I guess that's a typo):

它应该是模型而不是模型(没有尾随的,但我猜这是一个错字): 

class PickForm(ModelForm):
    class Meta:
        model = User

#2

 

只需执行此方法即可运行您的页面:

class PickForm(ModelForm):
  class Meta:
    model = Car
    fields = "__all__"

#3

 

如果这是一个副本和过去,你有一个错字。我强烈建议您使用IDE或错误检查。 Eclipse是我使用的。从这样的小烦恼中,它将为您节省大量时间。

class PickForm(ModelForm):
    class Meta:
        Model = User`

你的拼写错误就在汽车的尽头。小撇号的东西。

#4

 

您错过了将模型注册到管理员的基本步骤。请这样做,这应该适合你。

In the admin.py file of your app add these lines:

在应用的admin.py文件中添加以下行:

from yourapp.models import yourmodel
admin.site.register(yourmodel)

Here yourapp and yourmodel needs to be replaced with the correct names for your app and model.

在这里,您需要使用正确的应用程序和模型名称替换yourapp和yourmodel。

 

 我的错误就是格式 1 的问题:

class RegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password',widget=forms.PasswordInput)
    password2 =forms.CharField(label='Confirm Password',widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ("username", "email")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值