1

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

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 

class UserProfile(AbstractBaseUser, PermissionsMixin):

alphanumeric    = RegexValidator(r'^[0-9a-zA-Z]*$', message='Only alphanumeric characters are allowed.')
email           = models.EmailField(verbose_name='email address', unique=True, max_length=244)
username        = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
first_name      = models.CharField(max_length=30, null=True, blank=True)
last_name       = models.CharField(max_length=50, null=True, blank=True)
date_of_birth   = models.DateField(null=True);
date_joined     = models.DateTimeField(auto_now_add=True)
is_active       = models.BooleanField(default=True, null=False)
is_staff        = models.BooleanField(default=False, null=False)

profile_image   = models.ImageField(upload_to="profile")

objects         = MyUserManager()

USERNAME_FIELD  = 'email'
REQUIRED_FIELDS = ['username']

def get_full_name(self):
    fullname = self.first_name+" "+self.last_name
    return self.fullname

# def is_authenticated(self):
#   return True

def get_short_name(self):
    return self.username

def __str__(self):
    return self.email

我已经在字段类 Meta(UserCreationForm.Meta) 中输入了密码,但它仍然是同样的错误。

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

4

2 回答 2

1

尝试这样做可能会有所帮助

def create_user(self,email, password=None,**kwargs):

或者检查这些链接可能会帮助你想要什么。

链接1 链接 2

于 2015-05-28T11:39:03.500 回答
0

create_user() 只有 3 个参数,当你尝试给出更多或更少的参数时,就会出现这样的错误。例如,要避免此错误,请尝试这样-- create_user() 的定义--

def create_user(self, email, **kwargs):
    if not email:
        raise ValueError('Must have an email address')
    email = self.normalize_email(email)
    user = self.model(email=email, name=kwargs['name'])
    user.set_password(kwargs['password'])
    user.save(using=self._db)
    return user

调用 create_user()--

user = self.create_user(email, name=name, password=password)
于 2019-09-04T17:44:27.780 回答