5

Hello I am trying to create password reset view in Django. I have set up the mail backend and urls and templates. Everything looks fine but when I try to send mail to reset password Django is sending multiple emails. For example 7 or 11 password reset email at the same time. How can I make it just one email for each time.

Thanks a lot

This is What I did:

setting.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = "my_email"
EMAIL_HOST_PASSWORD = "my_password"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

urls.py

path('password-reset/',
     auth_views.PasswordResetView.as_view(template_name='accounts/password_reset.html'),
     name='password_reset'),
path('password-reset/done/',
     auth_views.PasswordResetDoneView.as_view(template_name='accounts/password_reset_done.html'),
     name='password_reset_done'),
path('password-set-confirm/<uidb64>/<token>/',
     auth_views.PasswordResetConfirmView.as_view(template_name='accounts/password_reset_confirm.html'),
     name='password_reset_confirm'),
path('password-set-complete/',
     auth_views.PasswordResetCompleteView.as_view(template_name='accounts/password_reset_complete.html'),
     name='password_reset_complete'),

Also, I set the templates for each view.

7
  • Welcome to SO. You need to show us a minimal reproducible example so we can understand what you're doing and where things go wrong. Commented Mar 17, 2019 at 12:30
  • Paste your code in the question using code formatting. Also the settings are probably not relevant here. You need to show the password reset view (and maybe the form) that handles the submission of the password reset form. Commented Mar 17, 2019 at 12:33
  • I did not solve wht but it sending multiple emails to my email for password resetting. Commented Mar 17, 2019 at 12:38
  • When I click the reset password it is waiting a while and it is sending many email to target email account. Then it is going to password-reset-done page Commented Mar 17, 2019 at 12:39
  • That should not happen. Check in your admin console if there are multiple users with the same email in your backend (that's possible if you didn't specify email to be unique). Or check User.objects.filter(email="[email protected]").count() in your console Commented Mar 17, 2019 at 12:42

4 Answers 4

9

IF this is still a problem... If you have multiple "test users" and you created them with the same email address that is probably causing your problme

Sign up to request clarification or add additional context in comments.

2 Comments

I am having the same problem. Haha
I couldn't figure out what was happening. When I saw this... Duh! 🙈
6

It sometimes helps to check the Django source code itself, so always dive into that if you don't understand what's happening.

If you look at PasswordResetForm's save() method in django.contrib.auth.forms, you'll see that it loops through self.get_users(email) and then sends one (and only one) email for each user.

So the only way multiple emails can be sent is if there are multiple users with the same email.

Comments

0

I don't think their is any setting for the same you may be end up calling same URL multiple time might be a logic issue. Try to use debug mode or with the help of print statements.

Comments

0

PasswordResetForm has get_users() method which returns all active usernames in an email. We can overwrite that method to return just 1 user which is passed to it. This way password reset email will be sent to only 1 user that we choose.

from django.contrib.auth.forms import PasswordResetForm


class MyPasswordResetFormSpecificUser(PasswordResetForm):
    """
    Send password reset email to specific user and not all active
    users in an email.
    """
    user = None

    def get_users(self, email):
        """
        Instead of getting all users in an email,
        just sent the user that we want to reset password for.
        """
        return [self.user]

Then you can use this new form class into your views as shown below:

email = 'your email here'
form = MyPasswordResetFormSpecificUser({'email': email})
assert form.is_valid()
form.user = 'User object who needs password reset'

# use https only when running on GAE
form.save(
    request=self.request,
    use_https=False # True if you are deploying to https url
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.