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

查看:32
本文介绍了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()

我尝试重新处理,但仍然遇到类似的异常,电子邮件地址匹配查询不存在."

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天全站免登陆