简体   繁体   中英

How to send password reset email from Django using django-rest-auth and Mailgun

I'm setting up user authentication in Django with Django rest auth. Most things are working well but I can't get the password reset emails to send, either in the dummy backend or with Mailgun.

I followed this tutorial to set up the basic auth. Everything in the tutorial works great but it doesn't explain how to do password reset.

I have an account with Mailgun and am trying to use the sandbox to send mails.

# api/urls.py
from django.urls import include, path

urlpatterns = [
    path('rest-auth/', include('rest_auth.urls')),
    path('rest-auth/registration/', include('rest_auth.registration.urls')),
    path('users/', include('users.urls')),
]

I'm requesting a password reset for a registered user via the browsable API form: http://127.0.0.1:8000/api/v1/rest-auth/password/reset/

I enter an email address in the form and press 'POST'. The page displays this:

POST /api/v1/rest-auth/password/reset/
HTTP 200 OK
Allow: POST, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "detail": "Password reset e-mail has been sent."
}

However the email is not sent!

The mailgun logs show no activity - no email has been sent.

When I look on the Network tab in the browser, I don't see a post request.

Here's my setup using Mailgun:

EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'postmaster@sandboxxxxx.mailgun.org'
EMAIL_HOST_PASSWORD = 'xxxx'
EMAIL_USE_TLS = True

(I've put my real sandbox and password in the actual file)

First, with this in settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

I expect the email text to be logged to the terminal where the server is running with .'/manage.py runserver. However nothing is logged.

Then, with this in settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

I would expect the emails to be sent by Mailgun.

What obvious dumb thing have I missed?

I think that Django rest auth includes a default email template so I shouldn't have to create that manually should I?

Is there something extra I have to do for the admin page at http://127.0.0.1:8000/api/v1/rest-auth/password/reset/ to do its thing? http://127.0.0.1:8000/api/v1/rest-auth/login/ works fine, I can log in and see my token returned from the server.

Very grateful for any ideas!

I don't have a good answer for this but I gave up on using django-rest-auth for password reset and am using the django-contrib-auth urls and templates instead. This doesn't fully match my React front end but at least it provides the user functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM