django-allauth:如何正确使用email_confirmed信号将用户设置为活动状态

时间:2014-07-17 17:03:44

标签: django django-admin django-authentication django-allauth

another post中,我提到我试图使用allauth的email_confirmed信号将确认用户的is_active字段更改为true。但是,以下代码给了我异常“用户匹配查询不存在。”

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匹配查询不存在。”

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.

这让我感到困惑,因为我可以检查用户表和我数据库中“电子邮件地址”表中的电子邮件地址字段,它们似乎都有匹配的电子邮件地址。帮助将不胜感激!

2 个答案:

答案 0 :(得分:14)

原来,email_confirmed信号返回的email_address实际上并不是一个包含地址的字符串,而是一个对象 - allauth.account.models.EmailAddress。文档中的内容并不是很清楚,但很高兴现在已经解决了。最终工作的代码是:

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

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

    user.save()

答案 1 :(得分:1)

我在尝试设置从AllAuth到我的CustomUser模型的信号时找到了此页面。

经过一些调试和继续搜索之后,我发现了一个关于该主题的旧GitHub线程:https://github.com/pennersr/django-allauth/issues/347。他的解决方案对我有用。

我将接收器功能的位置从app / signals.py更改为app / models.py

显然,默认情况下,AppConfig类的ready()函数会导入models.py文件,但需要将signals.py文件手动添加到apps.py文件中:

from django.apps import AppConfig


class NameOfAppConfig(AppConfig):
    name = 'NameOfApp'

    def ready(self):
        import NameOfApp.signals

无论如何,在文档中很容易错过这一点。我猜想在将来的Django版本中,它们会包含一个signal.py文件作为默认文件,并自动将其合并。