錯(cuò)誤是“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:當(dāng)我嘗試創(chuàng)建新的用戶表單 ( user_form = UserForm()) 時(shí)發(fā)生錯(cuò)誤。盡管在我的元類中我將模型設(shè)置為User從django.contrib.auth.models.
1 回答

慕哥9229398
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
我認(rèn)為您的表格中的拼寫錯(cuò)誤,您可以嘗試更正 :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')
添加回答
舉報(bào)
0/150
提交
取消