The password reset email was not sent

Hello everyone! I’m learning the Django authentication system. I was trying to build my own password reset page, but the backend not sent password reset emails. More details are as follows. (<mosaic> means sensitive information)

  1. I can send email by send_mail(), which means the following code works:
from django.core.mail import send_mail

send_mail(
    "Test",
    "Here is the message.",
    "hairlessvillager@<mosaic>.com",
    ["2542014055@<mosaic>.com"],
    fail_silently=False,
)

and the settings.py is as follows:

# ...
DEFAULT_FROM_EMAIL = "hairlessvillager@<mosaic>.com"
EMAIL_HOST = "smtp.<mosaic>.com"
EMAIL_HOST_USER = "hairlessvillager@<mosaic>.com"
EMAIL_HOST_PASSWORD = "kxu<mosaic>iea"
EMAIL_USE_SSL = True
EMAIL_PORT = 465
# ...
  1. The urls.py in app/ is as follows:
# ...
urlpatterns = [
    path("", views.index, name="index"),
    path("accounts/", include("django.contrib.auth.urls")),
    path("accounts/profile/", views.profile, name="profile"),
    path("policy/", views.policy, name="policy"),
]
  1. When there was no passwird_reset_form.html in registration/, url /accounts/password_reset/ was the default Django administration password reset page, and the email was sent successfully. But when I wrote the registration/passwird_reset_form.html as follows:
{% block content%}
    <p>Reset Password</p>
    <form method="get" action="{% url 'doctorhelper:password_reset_done' %}">   {# doctorhelper is the app_name#}
        {% csrf_token %}
        <table>
            <tr>
                <td>Email</td>
                <td>{{ form.email }}</td>
            </tr>
        </table>
        <input type="submit" value="Send Email!">
    </form>
{% endblock %}

the email was not sent, while the log was as follows:

[19/Dec/2023 13:15:26] "GET / HTTP/1.1" 200 10805
[19/Dec/2023 13:15:27] "GET /accounts/login/ HTTP/1.1" 200 10672
[19/Dec/2023 13:15:28] "GET /accounts/password_reset/ HTTP/1.1" 200 575
[19/Dec/2023 13:15:33] "GET /accounts/password_reset/done/?csrfmiddlewaretoken=0VxPAdvCaEEbonSsc9fJacVhAy0JJKp0H3O3eVtQPpag8IqR3qLHnAIxxG7qiKX0&email=2542014055%40<mosaic>.com HTTP/1.1" 200 10070

I’d tried to set the form method to “post” but it was not allowed:

Method Not Allowed (POST): /accounts/password_reset/done/
Method Not Allowed: /accounts/password_reset/done/
[19/Dec/2023 13:58:14] "POST /accounts/password_reset/done/ HTTP/1.1" 405 0

If any extra information is required, I’ll reply with it as soon as possible. Thanks for any help!

Wait a minute! Maybe I’ve found the solution: just change the

<form method="get" action="{% url 'doctorhelper:password_reset_done' %}">   {# doctorhelper is the app_name#}

to

<form method="post">

and the email is sent.

It’s a little confused that the <form> has no attribute “action”.

No, that’s actually the typical / common / default process cycle for forms in Django.

The same view that generates the page containing the form is the view that will process the form submittal. Then, when that processing is complete, the view redirects to the destination page.

1 Like