python - 配置了多个身份验证后端,因此必须提供 `backend` 参数或为用户设置 `backend` 属性

标签 python python-3.x django ubuntu oauth

首先,我是 django/python 的新手。

我正在尝试创建一个登录网站,允许用户注册一个帐户并通过电子邮件或直接登录验证 通过 fbgoogle(Oauth)

当我点击发送到 email 的验证 url 时,我收到错误消息。

错误:

ValueError at /activate/Mjk/4p1-dcc5f7ed2e7c847fe362/

You have multiple authentication backends configured and therefore must 

provide the `backend` argument or set the `backend` attribute on the user.

Request Method: GET

Request URL:    http://127.0.0.1:8000/activate/Mjk/4p1-dcc5f7ed2e7c847fe362/
Django Version: 1.11.3

Exception Type: ValueError

Exception Value:    

You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py in login, line 149
Python Executable:  /usr/bin/python
Python Version: 2.7.12
Python Path:    
['/home/gaby/django projects/simple-signup-master/profile-model',
 '/usr/local/lib/python2.7/dist-packages/virtualenv-15.1.0-py2.7.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/gaby/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time:    Wed, 30 Aug 2017 12:34:31 +0000

我的网站/设置

AUTHENTICATION_BACKENDS = (

    
     'social_core.backends.facebook.FacebookOAuth2',
     'social_core.backends.google.GoogleOAuth2',

     'django.contrib.auth.backends.ModelBackend',
)

这是我收到错误时调用的函数

    def activate(request, uidb64, token):
    try:
    uid = force_text(urlsafe_base64_decode(uidb64))
    user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
    user = None

    if user is not None and account_activation_token.check_token(user, 
    token):
    user.is_active = True
    user.profile.email_confirmed = True
    user.save()
    login(request, user)
    return redirect('home')
else:
    return render(request, 'account_activation_invalid.html')

在通过fbgoogle 添加oath 登录后,我开始收到错误。 如果我从 AUTHENTICATION_BACKENDS 中删除“social_core.backends.facebook.FacebookOAuth2”、“social_core.backends.google.GoogleOAuth2 电子邮件确认 将再次工作。

我在网上搜索过,我发现的唯一解决方案如下,但它没有解决我的问题。

here

最佳答案

我找到了解决方案。使用 login(request, user, backend='django.contrib.auth.backends.ModelBackend')

等后端参数调用 login()

完整代码如下:

def activate(request, uidb64, token, backend='django.contrib.auth.backends.ModelBackend'):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.profile.email_confirmed = True
        user.save()
        login(request, user, backend='django.contrib.auth.backends.ModelBackend')
        return redirect('home')
    else:
        return render(request, 'account_activation_invalid.html')

关于python - 配置了多个身份验证后端,因此必须提供 `backend` 参数或为用户设置 `backend` 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45961459/

相关文章:

python - 如何使用 pyparsing Group 和 SkipTo 进行文件解析?

python - Django ImageField.path 在 save 方法中返回没有 upload_to 的路径

python - 使用 Python 通过互联网访问网络摄像头

python - 在 Python Dataframe 中汇总行

python-3.x - 我可以使实例方法不可调用为静态方法吗?

python - 如何使用 pytest 断言不引发警告

python - 如何将 django-avatar 与我自己的用户模型一起使用,而不是与 django.contrib.auth.models.User 一起使用

python - 从同一网络上的另一台计算机访问 Django 网站

python - 在 Django 中查询全名

python - (python) 使用十六进制文字尝试转换为字符串、整数或任何其他内容