如何使用djanago-allauth发送post_save信号后发送电子邮件确认邮件

时间:2014-11-27 06:09:16

标签: python django django-allauth django-signals

我在我的网络应用中使用django-allauth进行帐户管理。

我有用户模型和UserProfile模型。

当用户注册时,它会创建一个用户(用户模型中的一个实例)。

UserProfile与模型用户相关联。

Allauth默认在用户注册时发送电子邮件,当用户注册时会向用户发送确认电子邮件。

现在我只想在填写UserProfile时发送该电子邮件。

我很想在这里使用信号。 当用户填写UserProfile时,它将调用post_save信号,该信号调用另一个函数user_signed_up_但我在将请求和用户args传递给该函数时遇到问题。

这是我的models.py

from django.db import models
from django.contrib.auth.models import User
from allauth.account.signals import user_signed_up
from allauth.account.utils import send_email_confirmation
from django.dispatch import receiver
import datetime

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)

    are_u_intrested = models.BooleanField(default=False)


#this signal will be called when user signs up.allauth will send this signal.

@receiver(user_signed_up, dispatch_uid="some.unique.string.id.for.allauth.user_signed_up")
def user_signed_up_(request, user, **kwargs):

    send_email_confirmation(request, user, signup=True)
    #this allauth util method, which sends confirmation email to user.





models.signals.post_save.connect(user_signed_up_, sender=UserProfile, dispatch_uid="update_stock_count")

#If user fills this userProfile , then I only send confirmation mail.
#If UserProfile model instace saved , then it send post_save signal.

1 个答案:

答案 0 :(得分:2)

你不需要在这里使用allauth的信号,你需要使用Django的模型信号。类似的东西:

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=UserProfile)
def userprofile_filled_out(sender, instance, created, raw, **kwargs):
    # Don't want to send it on creation or raw most likely 
    if created or raw:
        return

    # Test for existence of fields we want to be populated before
    # sending the email
    if instance.field_we_want_populated:
       send_mail(...)

希望有所帮助!