gpt4 book ai didi

Django 错误 : __init__() got multiple values for keyword argument 'max_length'

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

我遇到了这个错误。我不明白它的头和尾。

__init__() 为关键字参数“max_length”获取了多个值

我正在从 django.contrib.auth.formsUserCreationForm 添加三个字段,它们是 emailfirst namelast name,我想将它们保存到我的用户对象中。(名字和姓氏是否自动保存)。

这是我要加载的表单

class MyRegistrationForm(UserCreationForm):
#define fields
email=forms.EmailField(required=True)
first_name = forms.CharField(_('first name'), max_length=30, required=True)
last_name = forms.CharField(_('last name'), max_length=30, required=True)
helptext={'username':"* must contain only alphabets and numbers",
'email':"*",
'password1':"*must contain alphabets in upper and lower case, numbers special char",
'password2': "*Enter the same password as above, for verification"}

err_messages={'invalid_username': _("username must include only letters and numbers"),
'password_length': _("minimum length must be 8 characters"),
'password_invalid':_("must include special character")}

def __init__(self, *args, **kwargs):
super(MyRegistrationForm, self).__init__(*args, **kwargs)
for fieldname in ['username', 'password1', 'password2','email']:
self.fields[fieldname].help_text = self.helptext[fieldname]
self.error_messages.update(self.err_messages)




class Meta:
model=User
fields=('first_name','last_name','username','email','password1','password2')
#import pdb; pdb.set_trace()

def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
if not re.match(r'^\w+$',username):
raise forms.ValidationError(
self.error_messages['invalid_username'],
code='invalid_username',
)
return super(MyRegistrationForm, self).clean_username()


def clean_password2(self):
password1 = self.cleaned_data.get("password1")
if len(password1)<8:
raise forms.ValidationError(
self.error_messages['password_length'],
code='password_length',
)
if not (re.search(r'[a-z]', password1) and
re.search(r'[A-Z]', password1) and
re.search(r'[^a-zA-Z\d\s:;]',password1)):
raise forms.ValidationError(
self.error_messages['password_invalid'],
code='password_invalid',
)
return super(MyRegistrationForm, self).clean_password2()

def clean_email(self):
email = self.cleaned_data["email"]
try:
user = User.objects.get(email=email)
print user.email
print user.username
raise forms.ValidationError("This email address already exists. Did you forget your password?")
except User.DoesNotExist:
return email

def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.email=self.cleaned_data["email"]
if commit:
user.save()
return user

我读过这个article但这对我的情况没有帮助。

最佳答案

表单字段不是模型字段:它们不将位置参数作为冗长的名称。您需要将其指定为 label kwarg:

first_name = forms.CharField(label=_('first name'), max_length=30, required=True)

关于Django 错误 : __init__() got multiple values for keyword argument 'max_length' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543037/

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