django-allauth:如何正确使用email_confirmed信号将用户设置为活动 [英] django-allauth: how to properly use email_confirmed signal to set user to active

查看:174
本文介绍了django-allauth:如何正确使用email_confirmed信号将用户设置为活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一篇文章,我提到我试图使用allauth的email_confirmed信号将被确认用户的is_active字段更改为true。但是,以下代码给了我异常用户匹配查询不存在。

In another post, I mentioned that I was trying to use allauth's email_confirmed signal to change the is_active field on the confirmed user to true. However, the following code gave me the exception "User matching query does not exist."

from allauth.account.signals import email_confirmed
from django.dispatch import receiver
from django.contrib.auth.models import User

@receiver(email_confirmed)
def email_confirmed_(request, email_address, **kwargs):

    user = User.objects.get(email=email_address)
    user.is_active = True

    user.save()

我试图重新工作,仍然有一个类似的例外,EmailAddress匹配查询不存在。来自django.contrib.auth的django.dispatch导入接收器的


I tried to re-work this, and still got a similar exception, "EmailAddress matching query does not exist."

from allauth.account.signals import email_confirmed
from django.dispatch import receiver
from django.contrib.auth.models import User
from allauth.account.models import EmailAddress

@receiver(email_confirmed)
def email_confirmed_(request, email_address, **kwargs):

    new_email_address = EmailAddress.objects.get(email=email_address)
    user = User.objects.get(new_email_address.user)
    user.is_active = True

    user.save()

追溯在这里:

Environment:


Request Method: POST
Request URL: http://www.website.com/accounts/confirm-email/5901011619071fce757447ba146fe6312cb27bc0fee34d29665b857b479b49fc/

Django Version: 1.6.1
Python Version: 3.3.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'surveys',
'registration',
'django.contrib.sites',
'bootstrap3',
'allauth',
'allauth.account',
'allauth.socialaccount')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python3.3/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.3/dist-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.3/dist-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/usr/local/lib/python3.3/dist-packages/allauth/account/views.py" in post
  204.         confirmation.confirm(self.request)
File "/usr/local/lib/python3.3/dist-packages/allauth/account/models.py" in confirm
  111.                                          email_address=email_address)
File "/usr/local/lib/python3.3/dist-packages/django/dispatch/dispatcher.py" in send
  185.             response = receiver(signal=self, sender=sender, **named)
File "/home/username/website/webapp/mysite/views.py" in email_confirmed_
  84.     new_email_address = EmailAddress.objects.get(email=email_address)
File "/usr/local/lib/python3.3/dist-packages/django/db/models/manager.py" in get
  151.         return self.get_queryset().get(*args, **kwargs)
File "/usr/local/lib/python3.3/dist-packages/django/db/models/query.py" in get
  307.                 self.model._meta.object_name)

Exception Type: DoesNotExist at /accounts/confirm-email/5901011619071fce757447ba146fe6312cb27bc0fee34d29665b857b479b49fc/
Exception Value: EmailAddress matching query does not exist.

这对我来说很困惑,因为我可以检查用户表和我的数据库中的电子邮件地址表,他们似乎都有匹配的电子邮件地址。帮助将不胜感激!

This is confusing to me, because I can check the email address field in both the user table and the "email addresses" table in my database and they both seem to have matching email addresses. Help would be appreciated!

推荐答案

将email_confirmed信号返回的email_address实际上并不是包含地址的字符串,但是一个对象 - allauth.account.models.EmailAddress。这些文件根本不是很清楚,但很高兴现在解决。最终工作的代码是:

Turns out the email_address returned by the email_confirmed signal isn't actually a string containing the address, but an object -- allauth.account.models.EmailAddress. This wasn't very clear at all from the documentation, but glad it's resolved now. The code that ended up working was:

@receiver(email_confirmed)
def email_confirmed_(request, email_address, **kwargs):

    user = User.objects.get(email=email_address.email)
    user.is_active = True

    user.save()

这篇关于django-allauth:如何正确使用email_confirmed信号将用户设置为活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆