Tích hợp Chức năng Khôi phục Mật khẩu trên Django

Cập nhật giao diện đăng nhập bằng cách thêm liên kết khôi phục mật khẩu:

{% extends 'base.html' %}
{% load widget_tweaks %}
{% block content %}
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-5 col-sm-8">
        <h2 class="text-center mt-4 mb-3">Đăng nhập</h2>
        <hr>
        {% if error_message %}
          <div class="alert alert-danger">{{ error_message }}</div>
        {% endif %}
        <form method="post" action="{% url 'user_login' %}">
          {% csrf_token %}
          <div class="form-group">
            <label for="identifier">Tên người dùng</label>
            <input type="text" class="form-control" id="identifier" name="identifier" placeholder="Nhập tên người dùng">
          </div>
          <div class="form-group">
            <label for="pwd">Mật khẩu</label>
            <input type="password" class="form-control" id="pwd" name="password" placeholder="Nhập mật khẩu">
          </div>
          <div class="form-group text-right">
            <a href="{% url 'password_reset_request' %}" class="text-muted">Quên mật khẩu?</a>
          </div>
          <div class="form-group text-center">
            <button type="submit" class="btn btn-primary">Đăng nhập</button>
            <a href="{% url 'user_register' %}" class="btn btn-link">Đăng ký</a>
          </div>
        </form>
      </div>
    </div>
  </div>
{% endblock %}

Thiết lập các URL xử lý khôi phục mật khẩu trong urls.py:

# urls.py
from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('reset-password/', auth_views.PasswordResetView.as_view(), name='password_reset_request'),
    path('reset-password/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_sent'),
    path('reset-password/confirm/<token_id>/<reset_token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('reset-password/complete/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

Tạo các mẫu HTML cho quy trình:

  • registration/password_reset_request.html (mẫu yêu cầu khôi phục)
  • registration/password_reset_email.html (email thông báo)
  • registration/password_reset_sent.html (xác nhận gửi email)
  • registration/password_reset_confirm.html (xác nhận mật khẩu mới)
  • registration/password_reset_complete.html (hoàn tất khôi phục)

Cấu hình xử lý email trong settings.py:

# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = 'noreply@yourdomain.com'
EMAIL_HOST_PASSWORD = 'secure_password_here'
DEFAULT_FROM_EMAIL = 'noreply@yourdomain.com'

Xây dựng lớp xử lý tùy chỉnh:

# views.py
from django.contrib.auth.views import PasswordResetView
from django.urls import reverse_lazy

class PasswordResetHandler(PasswordResetView):
    template_name = 'registration/password_reset_request.html'
    email_template_name = 'registration/password_reset_email.html'
    success_url = reverse_lazy('password_reset_sent')

Thêm vào urls.py để sử dụng lớp tùy chỉnh:

# urls.py
from .views import PasswordResetHandler

urlpatterns = [
    path('reset-password/', PasswordResetHandler.as_view(), name='password_reset_request'),
    # Các URL khác
]

Thẻ: django-auth password-reset-flow email-configuration template-overrides

Đăng vào ngày 21 tháng 6 lúc 03:53