我正在尝试使用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")版权声明:本文为qq_38375620原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。