0

I am trying to add user password reset functionality in my Django app using its core password reset functionality, with the help of a tutorial from Corey MS's youtube channel. I did exactly what he did. I am getting no error, but no emails are being sent.

I have tried with my personal email server. I checked it using another python script using python smtplib and MIME, sending emails for me perfectly. But in Django, nothing happening.

Then, I have gone through several articles and QAs. At last, I set up a local email server using the command-line with this python -m smtpd -n -c DebuggingServer localhost:1025 command. But the result is the same. Command-line says an email sent. But its not actually.

Here are my screenshots and relevant codes.

my app says, email sent. my app says, email send

success message and project structure:

success message and project structure

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025

urls.py

from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from users import views as user_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register, name='register'),
    path('profile/', user_views.profile, name='profile'),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
    path('password-reset/',
         auth_views.PasswordResetView.as_view(
             template_name='users/password_reset.html'
         ),
         name='password_reset'),
    path('password-reset/done/',
         auth_views.PasswordResetDoneView.as_view(
             template_name='users/password_reset_done.html'
         ),
         name='password_reset_done'),
    path('password-reset-confirm/<uidb64>/<token>/',
         auth_views.PasswordResetConfirmView.as_view(
             template_name='users/password_reset_confirm.html'
         ),
         name='password_reset_confirm'),
    path('password-reset-complete/',
         auth_views.PasswordResetCompleteView.as_view(
             template_name='users/password_reset_complete.html'
         ),
         name='password_reset_complete'),
    path('', include('blog.urls')),
]

password_reset.html

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Reset Password</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Request Password Reset</button>
            </div>
        </form>
    </div>
{% endblock content %}

password_reset_done.html

{% extends "blog/base.html" %}
{% block content %}
    <div class="alert alert-info">
        An email has been sent with instructions to reset your password
    </div>
{% endblock content %}

password_reset_confirm.html

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Reset Password</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Reset Password</button>
            </div>
        </form>
    </div>
{% endblock content %}

password_reset_complete.html

{% extends "blog/base.html" %}
{% block content %}
    <div class="alert alert-info">
        Your password has been set.
    </div>
    <a href="{% url 'login' %}">Sign In Here</a>
{% endblock content %}

Please comment if I need to add more details. Please let me know, why my code is not working? what else I need to do?

2 Answers 2

2

First i would test your email settings with a simple test mail doku. At my server i set following varibles in the settings.py file:

  • EMAIL_HOST,
  • EMAIL_PORT,
  • EMAIL_HOST_USER
  • EMAIL_HOST_PASSWORD
  • EMAIL_USE_TLS
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I tried it already. It is working when sending it from the command line. But not working in my code.
1

Since we are using the auth users of Django, then all we know is that the User model comes with the built in Django view, then if we are using the email address for receiving Django reset password link, we should write the same email that we registered when we create account.

EMAIL_HOST_USER : '[email protected]'  

This email should be the same as the one you registered with, I mean the one that you use when you registered and that is in the database under using of a specific user. So use this email also when you are sending a password reset from Django to the Gmail server.

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.