【发布时间】:2025-12-12 06:45:02
【问题描述】:
我正在使用 django allauth 进行社交登录/注册。另外,我有自己的注册表单作为备用登录/注册。以下是我以备用形式从用户那里获取的字段。
class Profile(models.Model):
col1 = models.CharField(max_length=50, blank=True, null=True)
col2 = models.CharField(max_length=50, blank=True, null=True)
user = models.OneToOneField(User)
因此,当用户注册时,除了username、email 和password 之外,它还要求提供其他字段(col1、col2)。
以下是注册视图。
user = User.objects.create_user(username, user_email, user_pass)
Profile.objects.create(user=user, col1=col1, col2=col2)
return
因此,每当用户通过备用表单注册时,就会调用上述视图。
现在,相比之下,当用户从社交帐户 FB 注册时,它不会要求额外的信息,即col1/col2。它直接注册而不要求额外信息,我也不希望它问。
然后我使用信号在Profile 模型发布注册中创建一行。
@receiver(user_signed_up)
def create_profile(request, user, sociallogin=None, **kwargs):
if sociallogin:
if sociallogin.account.provider == 'facebook':
data = sociallogin.account.extra_data
col1 = data.get('col1')
col2 = data.get('col2')
Profile.objects.create(user=user, col1=col1, col2=col2)
所以,(1) 我的问题是当使用替代形式创建用户时,allauth 表中没有插入任何记录,我觉得这很奇怪。
(2) 考虑一下,我使用 E1 作为电子邮件 ID 使用备用表单进行了注册。现在我通过allauth(FB)使用相同的id注册,它会抛出一个错误。
(3) 如何向使用all_auth 以替代形式注册的用户发送确认邮件。
【问题讨论】:
-
为什么要投反对票???
-
我不是反对者。你能解释一下你是如何保存
extra_data的吗? -
@RajaSimon - 问题中提供了一个演示 sn-p
create_profile。请看一看。谢谢。 :) -
作为一个良好的实践通知,不要在字符串上允许空值,而是使用 (blank=True, default='', max_length=50)
标签: django django-allauth django-registration