Sunday, 5 May 2019

django 33 reset password by email

click reset password 

enter email address, django looks through users' record. If match is found, reset email will be sent.
if email is same with others, django sends email to user registered first. make sure email is unique.

notification email is sent

mail box receives email, click the link

enter new password

password changed, click log in

log in with new password

log in successful


#project1/urls

from django.contrib import admin
from django.urls import path, include
from django.conf import  settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('music/', include('music.urls')),
    path('', include('django.contrib.auth.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

----------------------------------------
#settings

#email setting
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'zchen2014chuanshuo@gmail.com'
EMAIL_HOST_PASSWORD = 'xxx'

----------------------------------------
#templates/registration/password_reset_done

{% block content %}
  <p>
    We've emailed you instructions for setting your password, if an account exists with the email you entered.
    You should receive them shortly.
  </p>
  <p>
    If you don't receive an email, please make sure you've entered the address you registered with,
    and check your spam folder.
  </p>
{% endblock %}

---------------------------------------
#templates/registration/password_reset_email

{% autoescape off %}
To initiate the password reset process for your {{ user.get_username }} TestSite Account,
click the link below:

{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.

Sincerely,
The TestSite Team
{% endautoescape %}

---------------------------------------
#templates/registration/password_reset_confirm

{% block content %}
  {% if validlink %}
    <h3>Change password</h3>
    <form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Change password</button>
    </form>
  {% else %}
    <p>
      The password reset link was invalid, possibly because it has already been used.
      Please request a new password reset.
    </p>
  {% endif %}
{% endblock %}

---------------------------------------------------
#templates/registration/password_reset_complete

{% block content %}
  <p>
    Your password has been set. You may go ahead and <a href="{% url 'signin' %}">sign in</a> now.
  </p>
{% endblock %}

---------------------------------------
#templates/music/login_form

{% extends 'music/base.html' %}
{% block title %}Log in{% endblock %}
{% load crispy_forms_tags %}

{% block body %}
    <div class="container-fluid">

        <div class="row">

            <div class="col-sm-12 col-md-7">

                <form  action="" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <fieldset class="form-group">
                        <legend class="border-bottom mb-4">Log in</legend>
                    </fieldset>
                    <div class="form-group">
                        {{ form|crispy }}
                    </div>
                    <div class="form-group">
                        Don't have an account?
                        <a href="{% url 'music:register' %}">Register one</a>
                    </div>
                    <div class="form-group">
                        Forgot password? Reset by email.
                        <a href="{% url 'password_reset' %}">Reset password</a>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-success btn-sm" type="submit">Submit</button>
                    </div>
                </form>


            </div>
        </div>
    </div>

{% endblock %}

-------------------------------------
#templates/registration/password_reset_subject

TestSite password reset

reference:
https://stackoverflow.com/questions/44676880/error-reverse-for-password-reset-done-not-found-password-reset-done-is-not
https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html

No comments:

Post a Comment