使用default_token_generator的Django Allauth错误

时间:2016-07-07 21:43:54

标签: python django django-allauth

我正在尝试生成默认令牌以发送重置密码电子邮件,但是当代码运行 default_token_generator.make_token(用户)时,用户没有 last_login 属性。我在这里做错了什么?

/ devices / 0 /上的AttributeError忘记了 'QuerySet'对象没有属性'last_login'

            user = User.objects.filter(email__iexact=email)
            if Provision.objects.get(device=device, user=user):
                # email_address = EmailAddress.objects.get_primary(user=user)
                token = default_token_generator.make_token(user) ...
                site = Site.objects.get_current()
                # send the password reset email
                path = reverse("account_reset_password_from_key", kwargs=dict(uidb36=int_to_base36(user.pk), key=token))
                url = '%s://%s%s' % (app_settings.DEFAULT_HTTP_PROTOCOL, site.domain, path)

2 个答案:

答案 0 :(得分:1)

user = User.objects.filter(email__iexact=email) - filter()中返回QuerySet,make_token()需要用户模型实例。请改为user = User.objects.get(email__iexact=email)

答案 1 :(得分:0)

在您的MIDDLEWARE_CLASSES部分提供

django.contrib.auth.middleware.AuthenticationMiddleware',
列出了

,因为这是为Django用户提供此属性的,因此产生的MIDDLEWARE_CLASSES就像

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
]