无法导入名称save_status_to_session

时间:2018-04-13 13:55:32

标签: python django django-socialauth

我在我的网站上使用python-social-auth API对facebook进行身份验证。但我得到了例外。

使用' apps.pipeline.save_profile'

'无法导入名称save_status_to_session'

我正在使用django-pipeline版本1.6.13

此代码在pipelne.py

def require_email(strategy, details, user=None, is_new=False, *args, **kwargs):
    if kwargs.get('ajax') or user and user.email:
        return
    elif is_new and not details.get('email'):
    email = strategy.request_data().get('email')
    if email:
        details['email'] = email
    elif email is None:
        error = "Sorry, but your social network (Facebook or Google) 
                 needs to provide us your email address."
        return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + "?error=" + urllib.quote_plus(error))
    else:
        return redirect('require_email')


def save_profile(backend, details, response,  uid,user, *args, **kwargs):
    if backend.name == 'facebook':
      url = "http://graph.facebook.com/%s/picture?type=large" % response['id']
      avatar = urlopen(url)
      profile = Profile.objects.filter(user_id=user.id)
      if profile.count()==0:
        profile = Profile(user_id=user.id)
      else:
        profile = Profile.objects.get(user_id=user.id)
        profile.profile_image.save(slugify(user.username + " social") + '.jpg', 
                        ContentFile(avatar.read()))              
        profile.save()

    elif backend.name == 'google-oauth2':
        if response.get('image') and response['image'].get('url'):
            url = response['image'].get('url')
            ext = url.split('.')[-1]
            profile = Profile.objects.filter(user_id=user.id)
            if profile.count()==0:
                profile = Profile(user_id=user.id)

            else:
              profile = Profile.objects.get(user_id=user.id)
              profile.profile_image.save(
              '{0}.{1}'.format(user.username, ext),
              ContentFile(urllib2.urlopen(url).read()),
              save=False
        )
              profile.save()

和设置

SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.get_username',
'polls.pipeline.require_email',
'social.pipeline.user.create_user',
'polls.pipeline.save_profile',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',

enter image description here

1 个答案:

答案 0 :(得分:0)

根据您提供的信息量非常有限,很难给出正确的答案。我会根据您链接的屏幕截图尝试一下。

首先,似乎尝试从文件save_status_to_session中的social_core.pipeline.partial导入social/pipeline/partial.py。导入错误并不奇怪,因为social_core.pipeline.partial没有定义这些函数(请参阅https://github.com/python-social-auth/social-core/blob/master/social_core/pipeline/partial.py)。

文件social/pipeline/partial.py似乎对应于不再维护的模块(请参阅https://github.com/omab/python-social-auth/blob/master/social/pipeline/partial.py和存储库根目录下的README)。

当您在mysite/polls/pipeline.py中导入该模块时,我建议您将导入from social.pipeline.partial import partial替换为from social_core.pipeline.partial import partial