Search code examples
pythondjangoif-statementdjango-modelsdjango-signals

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.

update:

My view function for registering user may be helpful for solving the issue. Here is the view function:

def signup(request):
    """
    Render signup form if request is GET else create user
    """
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()
    return render(request, 'registration/signup.html', {'form': form})

Solution

  • Assuming that you are using Django's built-in registration flow this will imply 2 actions on the User model:

    1. It will create the User object, thus firing the post_save signal with created=True and you will be sending the first email.
    2. After registration, Django logs in the user and updates it's last_login field, thus firing another post_save signal with created=False