为了账号安全,请及时绑定邮箱和手机立即绑定

Django ModelForm 没有指定模型类,虽然 Meta 类中的模型设置为模型

Django ModelForm 没有指定模型类,虽然 Meta 类中的模型设置为模型

茅侃侃 2023-06-13 19:15:46
错误是“ModelForm 没有指定模型类。”表格.py代码from django.contrib.auth.models import Userfrom basic_app.models import UserProfileInfoclass UserForm(forms.ModelForm):    password = forms.CharField(widget=forms.PasswordInput())    class Meta():        models = User        fields = ('username', 'email', 'password')class UserProfileInfoForm(forms.ModelForm):    class Meta():        model = UserProfileInfo        fields = ('portfolio_site', 'profile_pic')模型.py代码from django.db import modelsfrom django.contrib.auth.models import User# Create your models here.class UserProfileInfo(models.Model):    user = models.OneToOneField(User, on_delete=models.CASCADE)    #additional    portfolio_site = models.URLField(blank=True)    profile_pic = models.ImageField(upload_to='profile_pics', blank=True)    def __str__(self):        return self.user.usernameviews.py 代码from django.shortcuts import renderfrom basic_app.forms import UserProfileInfoForm, UserForm# Create your views here.def index(request):    return render(request, 'basic_app/index.html')def register(request):    registered = False    if request.method == 'POST':        user_form = UserForm(data=request.POST)        profile_form = UserProfileInfoForm(data=request.POST)        if user_form.is_valid() and profile_form.is_valid():            user = user_form.save()            user.set_password(user.password)            user.save()            profile = profile_form.save(commit=False)            profile.user = user            if 'profile_pic' in request.FILES:                profile.profile_pic = request.FILES['profile_pic']            profile.save()            registered = True        else:当我尝试创建新的用户表单 ( user_form = UserForm()) 时发生错误。尽管在我的元类中我将模型设置为User从django.contrib.auth.models.
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

我认为您的表格中的拼写错误,您可以尝试更正 :Changes Meta() to Meta: then models to model


class UserForm(forms.ModelForm):

   password = forms.CharField(widget=forms.PasswordInput())


   class Meta:

      model = User

      fields = ('username', 'email', 'password')


class UserProfileInfoForm(forms.ModelForm):

   class Meta:

      model = UserProfileInfo

      fields = ('portfolio_site', 'profile_pic')


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信