使用django allauth自定义用户

2 投票
1 回答
1332 浏览
提问于 2025-04-18 08:46

我正在尝试在django-allauth/social auth中使用自定义用户。 在settings.py文件中,我有以下设置:

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    "django.contrib.auth.backends.ModelBackend",

    # `allauth` specific authentication methods, such as login by e-mail
    "allauth.account.auth_backends.AuthenticationBackend",
)

SOCIALACCOUNT_PROVIDERS = \
    {'facebook':
       {'SCOPE': ['email', 'user_likes', 'user_status', 'user_about_me', 'basic_info', 'read_stream'],
        'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
        'METHOD': 'oauth2',
        'LOCALE_FUNC': 'path.to.callable',
        'VERIFIED_EMAIL': False}}


LOGIN_REDIRECT_URL = '/results/' 
AUTH_USER_MODEL = 'users.User'

在项目文件夹中的users文件夹里,我有一个adapter.py文件:

from django.conf import settings
from allauth.account.adapter import DefaultAccountAdapter

class MyAccountAdapter(DefaultAccountAdapter):

def get_login_redirect_url(self, request):
    path = "/results/{username}/"
    return path.format(username=request.user.username)

在models.py文件中,我有:

from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    user_data = models.TextField(null=True)

当我尝试用Facebook登录时,我会被重定向到Facebook,但返回到网站后,我看到以下错误信息:

Django version 1.6.2
Exception Type: DoesNotExist
User matching query does not exist.

在控制台中显示:

"GET /results/facebook/login/? HTTP/1.1" 302 0
"GET /results/facebook/login/callback/XXX HTTP/1.1" 500

你知道我哪里出错了吗?

1 个回答

0

我觉得你可能是在用邮箱找用户名,或者反过来。试着这样指定一下:

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'

撰写回答