IndexError: list index out of range, django-allauth

IndexError: list index out of range, django-allauth

我想做什么?

我正在尝试从社交帐户访问数据。为此,我正在关注 this 网站。

问题是什么?

成功登录我的 Facebook 帐户后出现以下错误:

错误: IndexError at /accounts/facebook/login/callback/

list index out of range

在互联网上查找我看到 post 并尝试实施它,但结果是 keyerror 'user'.

我的代码:

adaptar.py:

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.socialaccount.models import SocialAccount

class CustomAdapter(DefaultSocialAccountAdapter):

   def pre_social_login(self, request, sociallogin):
      account_uid = SocialAccount.objects.filter(user_id=request.user.id, provider='facebook')
      print account_uid[0].extra_data['first_name']

settings.py:

SOCIALACCOUNT_ADAPTER = 'Authentication.adapter.CustomAdapter' 

终于找到解决方法:

我没有使用 SocialAccount 模型来获取用户数据,而是使用传递的参数 sociallogin 来获取用户数据,如下所示:

解决方法:

adaptar.py:

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter

class CustomAdapter(DefaultSocialAccountAdapter):   
      def pre_social_login(self, request, sociallogin):
         if sociallogin.account.provider == 'facebook':
            first_name = sociallogin.account.extra_data['first_name'] 
            print first_name 

更改为:

 account_uid = SocialAccount.objects.filter(user_id=request.user.id, provider='facebook')

到这个:

if sociallogin.account.provider == 'facebook':
   first_name = sociallogin.account.extra_data['first_name']