lollercoaster lollercoaster - 1 month ago 21
Python Question

django 1.7.8 not sending emails with password reset

Relevant part of

urls.py
for the project:

from django.conf.urls import include, url, patterns
urlpatterns = patterns('',

# other ones ...

url(r'^accounts/password/reset/$',
'django.contrib.auth.views.password_reset',
{'post_reset_redirect' : '/accounts/password/reset/done/'}),

url(r'^accounts/password/reset/done/$',
'django.contrib.auth.views.password_reset_done'),

url(r'^accounts/password/reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
{'post_reset_redirect' : '/accounts/password/done/'}),

url(r'^accounts/password/done/$',
'django.contrib.auth.views.password_reset_complete'),

)


And by request, here's the password reset form:

{% extends "site_base.html" %}

{% block title %}Reset Password{% endblock %}

{% block content %}
<p>Please specify your email address to receive instructions for resetting it.</p>

<form action="" method="post">
<div style="display:none">
<input type="hidden" value="{{ csrf_token }}" name="csrfmiddlewaretoken">
</div>
{{ form.email.errors }}
<p><label for="id_email">E-mail address:</label> {{ form.email }} <input type="submit" value="Reset password" /></p>
</form>
{% endblock %}


But whenever I navigate to the
/accounts/password/reset/
page and fill in email and click enter the page immediately redirects to
/accounts/password/reset/done/
and no email is sent.

My relevant
settings.py
variables:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'XXX@gmail.com'
EMAIL_HOST_PASSWORD = 'XXXXXX'
EMAIL_PORT = 587

DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER


And I know email works because my registration flow with
django-registration-redux
works flawlessly.

Any ideas?

Answer

I tried to recreate your situation and I faced the following scenarios:

  1. Mail is only sent to active users. Email associated with no user will not get any email(obviously).
  2. I got an error form's save method in line 270 for email = loader.render_to_string(email_template_name, c):

NoReverseMatch at /accounts/password/reset/ Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{'token': '42h-4e68c02f920d69a82fbf', 'uidb64': b'Mg'}' not found. 0 pattern(s) tried: []

It seems that your urls.py doesn't contain any url named 'password_reset_confirm'. So you should change your url:

url(r'^accounts/password/reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
  'django.contrib.auth.views.password_reset_confirm',
 {'post_reset_redirect': '/accounts/password/done/'},),

To:

url(r'^accounts/password/reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
  'django.contrib.auth.views.password_reset_confirm',
 {'post_reset_redirect': '/accounts/password/done/'}, name='password_reset_confirm'),

If you have set your email configuration perfectly the you should get emails with no problem. If still you are facing this issue, please use a debugger to check where its getting exceptions.

PS: I have tested with django 1.7.8 and templates resides in: Python34\Lib\site-packages\django\contrib\admin\templates\registration. Urls and views are used as you have written in the question.