IndexError:列表索引超出范围,django-allauth

时间:2018-01-09 10:50:20

标签: django django-allauth

我想做什么?

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

有什么问题?

成功登录我的Facebook帐户后,我收到以下错误:

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

list index out of range

在互联网上查看我发现了this帖子,并试图实施它,但这给了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' 

1 个答案:

答案 0 :(得分:0)

终于找到了解决方案:

我没有使用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']