gpt4 book ai didi

python - 表单配置不正确 - Django

转载 作者:行者123 更新时间:2023-12-01 04:15:15 24 4
gpt4 key购买 nike

最近,我弄清楚了如何在 Django 中创建注册。问题是只有三个字段 - 用户名密码及其确认。

在我的项目中,至少有两种类型的具有不同属性的用户客户自由职业者。所以我尝试创建新模型,就像我在 StackOverflow 上找到的那样,但它返回异常:

Exception Type: ImproperlyConfigured
Exception Value:
Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form UserCreateForm needs updating.

模型.PY:

class UserCustomerProfile(models.Model):
user = models.OneToOneField(User)
email = models.EmailField()
age = models.IntegerField()

def __str__(self):
return "%s's profile" % self.user

FORMS.PY:

class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)

class Meta:
model = User

def save(self, commit=True):
if not commit:
raise NotImplementedError("Can't create User and UserProfile without database save")
user = super(UserCreateForm, self).save(commit=True)
user_profile = UserCustomerProfile(user=user, email=self.cleaned_data['email'])
user_profile.save()
return user, user_profile

VIEWS.PY:

def register(request):
if request.method == 'POST':
form = UserCustomerProfile(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('register/complete/')

else:
form = UserCreateForm()
token = {}
token.update(csrf(request))
token['form'] = form

return render_to_response('register/registration_form.html', token)

你知道如何解决这个问题吗?

最佳答案

Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form UserCreateForm needs updating.

尝试例如:

class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)

class Meta:
model = User
fields = ['email', ]

关于python - 表单配置不正确 - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34394116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com