gpt4 book ai didi

python - Django 社交授权

转载 作者:行者123 更新时间:2023-11-30 23:10:55 25 4
gpt4 key购买 nike

我想进行登录社交身份验证,并且使用用户配置文件进行登录,但出现错误。这是错误:

Exception Type: TypeError at /complete/facebook/
Exception Value: create_user() takes at least 3 arguments (2 given)

这是我的 admin.py 代码

class CustomUserCreationForm(UserCreationForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password Confirmation', widget=forms.PasswordInput)

class Meta(UserCreationForm.Meta):
model = UserProfile
fields = ('username', 'email','password')

def clean_username(self):
username = self.cleaned_data["username"]

try:
UserProfile._default_manager.get(username=username)
except UserProfile.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")

if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords do not match.")
return password2

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

这是模型.py

class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')

user = self.model(
username = username,
email = self.normalize_email(email),
)
user.is_active = True
user.set_password(password)
user.save(using=self._db)
return user

# def create_user(self, username, email):
# return self.model._default_manager._create_user(username=username)

def create_superuser(self, username, email, password):
user = self.create_user(username=username, email=email, password=password)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user

我已经将密码放入字段类 Meta(UserCreationForm.Meta) 中,但它仍然是相同的错误。

你能帮我解决这个问题吗?

最佳答案

正如错误所提到的,create_user() 需要至少三个参数,但在定义字段时仅提供两个参数(用户名和电子邮件)。 Django documentation很好地涵盖了这一点。尝试添加密码字段并查看它是否有效。

关于python - Django 社交授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30449493/

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