-1

Lets say i have an email when a user logs in . Could there a way to recieve an email that {user's email} has logged in and for log outs too.

I have tried to do reaserch on this but didnt know how to start as i am a new to making websites

1 Answers1

0

You can use the built-in signals from Django to achieve this.

from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver


@receiver(user_logged_in)
def mail_on_login(sender, request, user, **kwargs):
    send_login_mail()

@receiver(user_logged_out)
def mail_on_login(sender, request, user, **kwargs):
    send_logout_mail()

Check out the documentation https://docs.djangoproject.com/en/4.1/ref/contrib/auth/.

Marco
  • 2,371
  • 2
  • 12
  • 19