错误是“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')
添加回答
举报
0/150
提交
取消