Skip to content

Instantly share code, notes, and snippets.

@supawaza
Last active November 15, 2019 18:15
Show Gist options
  • Save supawaza/ab395004a75c2d9303fea6f60f2b3400 to your computer and use it in GitHub Desktop.
Save supawaza/ab395004a75c2d9303fea6f60f2b3400 to your computer and use it in GitHub Desktop.
Django / Wagtail: send password reset email after creating user. This will automatically sends a password reset email once a user has been successfully created in wagtail or django admin. You can also check the type of user and send the specific the email depending on the type of user. This is useful for when you are adding user inside the Admin…
from django.contrib.auth.forms import PasswordResetForm
from wagtail.core import hooks
# Totally can use the same thing for django signal as well
@hooks.register('after_create_user')
def send_reset_password_email(request, user):
reset_password_form = PasswordResetForm(
data={'email': user.email}
)
if reset_password_form.is_valid():
if user.is_admin:
# Send your custom email
reset_password_form.save(
request=request,
subject_template_name='account/email/admin_password_reset_subject.txt',
email_template_name='account/email/admin_password_reset.txt'
)
else:
reset_password_form.save(
request=request,
subject_template_name='account/email/password_reset_subject.txt',
email_template_name='account/email/password_reset.txt'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment