gpt4 book ai didi

django - 如何正确结合 Django-Allauth 和自定义用户配置文件应用程序?

转载 作者:行者123 更新时间:2023-12-05 05:10:50 26 4
gpt4 key购买 nike

我创建了一个名为 users 的新应用,其模型为 Profile。对于身份验证,我将 django-allauthFacebookGoogle 提供商一起使用。用户登录后,我想创建一个配置文件,其中包含一些来自社交提供商的附加信息,例如:全名、电子邮件、图片

这是我在 models.py

中的内容:
from django.contrib.auth.models import User
from django.dispatch import receiver
from allauth.account.signals import user_signed_up


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(default=None, max_length=255)
email = models.CharField(default=None, max_length=500)
picture = models.ImageField(default='default.jpg', upload_to='profile_pics')

def __str__(self):
return self.user.username

@receiver(user_signed_up)
def populate_profile(sociallogin, user, **kwargs):

user.profile = Profile()

if sociallogin.account.provider == 'facebook':
user_data = user.socialaccount_set.filter(provider='facebook')[0].extra_data
picture_url = "http://graph.facebook.com/" + sociallogin.account.uid + "/picture?type=large"
email = user_data['email']
full_name = user_data['name']

if sociallogin.account.provider == 'google':
user_data = user.socialaccount_set.filter(provider='google')[0].extra_data
picture_url = user_data['picture']
email = user_data['email']
full_name = user_data['name']

user.profile.picture = picture_url
user.profile.email = email
user.profile.full_name = full_name
user.profile.save()

在使用 Facebook 登录时,我收到以下错误消息:

[WinError 10061] No connection could be made because the target machine actively refused it

当我尝试使用 Google 登录时,我收到以下信息:

DataError at /accounts/google/login/callback/ value too long for type character varying(100)

有人可以告诉我我的代码有什么问题吗?提前致谢。

最佳答案

问题现已解决。我通过添加解决的第一条错误消息:

ACCOUNT_EMAIL_VERIFICATION = None

对于第二个,我必须为图片添加 ma​​x_length=255:

picture = models.ImageField(default='default.jpg', upload_to='profile_pics', max_length=255)

关于django - 如何正确结合 Django-Allauth 和自定义用户配置文件应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192922/

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