lundi 22 avril 2019

Why else block is executing while if block is true in Django signals?

I am writing a signal function that will send a signup notification email to the user when s/he signs up and an update notification email when s/he updates the user profile. For that, I have used an if-else block to check the created parameter. If it is true, the user will get signup notification. Otherwise, the user will get a profile update notification. It works fine when the user object is updated. But when a new user signs up, both of the if and else blocks are executing and the new signed up user gets two email notification: one signup notification and one profile update notification. The full signal code is given below:

#myapp/signals.py

from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils.html import strip_tags

#getting custom user model
User = get_user_model()


@receiver(post_save, sender=User)
def send_user_notification(sender, instance, created, **kwargs):
    if created:
        subject = 'Signup Notification'
        html_message = render_to_string(
            'email/signup_notification.html',
            {
                'username': instance.username,
                'email': instance.email,
            }
        )
        message = strip_tags(html_message)
        send_mail(subject, message, from_email='info@silatechnologies.com', recipient_list=[instance.email,],
                  html_message=html_message)

    else:
        send_mail(subject='Your profile on Sila Crowdfunding updated!',
                  message='Dear ' + instance.username + ', your profile information is updated successfully.',
                  from_email='info@silatechnologies.com',
                  recipient_list=[instance.email, ]
                  )

I have also used if not created instead of else. But I am getting the same result. Can anyone provide any solution of that? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire