如何将Django密码重置功能指向自定义模板?
Your current setup for the initial password reset form is on the right track! Let’s walk through the full process to ensure every stage of the password reset flow uses your custom templates, and fix any potential gaps.
1. Update urls.py for the Complete Reset Flow
The password reset process has four core views—you’ll need to map each to your custom templates. Here’s the adjusted code using Django’s recommended class-based views:
from django.contrib.auth import views as auth_views from django.urls import path urlpatterns += [ # Initial password reset request form path("reset-password/", auth_views.PasswordResetView.as_view( template_name="pages/password_reset_form.html", success_url="/reset-password/done/" ), name="reset_pw"), # Success message after submitting reset request path("reset-password/done/", auth_views.PasswordResetDoneView.as_view( template_name="pages/password_reset_done.html" ), name="password_reset_done"), # Password reset confirmation page (from email link) path("reset/<uidb64>/<token>/", auth_views.PasswordResetConfirmView.as_view( template_name="pages/password_reset_confirm.html", success_url="/reset-password/complete/" ), name="password_reset_confirm"), # Final confirmation page after password is reset path("reset-password/complete/", auth_views.PasswordResetCompleteView.as_view( template_name="pages/password_reset_complete.html" ), name="password_reset_complete"), ]
Key Notes:
- Class-based views (
PasswordResetViewinstead of the old function-basedpassword_reset) are more flexible and align with modern Django practices. - Explicit
success_urlvalues ensure users move through the flow correctly. - The
uidb64andtokenparameters in the confirmation URL are critical for Django to verify the user’s identity.
2. Customize Your Templates
You already have password_reset_form.html—let’s refine it and create the other required templates.
password_reset_form.html
Your hardcoded input works, but using Django’s built-in form will automatically handle error messages and validation. Here’s an improved version:
<form id="reset-pw-form" name="resetPWForm" method="post" action="{% url 'reset_pw' %}"> {% csrf_token %} {% if form.errors %} <div class="alert alert-error"> {% for field in form %} {% for error in field.errors %} <p>{{ error }}</p> {% endfor %} {% endfor %} </div> {% endif %} <input id="id_email" type="email" placeholder="Enter email" name="email" required> <button type="submit">Submit</button> </form>
If you prefer to keep your original input, just ensure the name attribute stays email so Django can process it correctly.
password_reset_done.html
This template tells users to check their email for reset instructions:
<h2>Password Reset Sent</h2> <p>We’ve emailed you instructions to reset your password. If an account exists with the email you entered, you’ll receive them shortly.</p> <p>If you don’t see the email, check your spam folder or verify you entered the correct address.</p>
password_reset_confirm.html
This is where users set their new password:
<h2>Set New Password</h2> <form method="post"> {% csrf_token %} {% if form.errors %} <div class="alert alert-error"> {% for field in form %} {% for error in field.errors %} <p>{{ error }}</p> {% endfor %} {% endfor %} </div> {% endif %} <input id="id_new_password1" type="password" placeholder="New password" name="new_password1" required> <input id="id_new_password2" type="password" placeholder="Confirm new password" name="new_password2" required> <button type="submit">Update Password</button> </form>
password_reset_complete.html
This confirms the password reset was successful:
<h2>Password Reset Complete</h2> <p>Your password has been reset successfully. You can now <a href="{% url 'login' %}">log in</a> with your new password.</p>
3. Optional: Customize the Reset Email
To tweak the email sent to users, add email_template_name to the PasswordResetView in urls.py:
path("reset-password/", auth_views.PasswordResetView.as_view( template_name="pages/password_reset_form.html", success_url="/reset-password/done/", email_template_name="pages/password_reset_email.html" # Add this line ), name="reset_pw"),
Then create pages/password_reset_email.html:
Hi there, Someone requested a password reset for your account using email {{ email }}. To reset your password, click the link below: {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} If you didn’t make this request, you can safely ignore this email. Thanks, Your Team
Final Checks
- Ensure all template files are in the correct directory (
templates/pages/—confirm yoursettings.pyincludes'DIRS': [BASE_DIR / 'templates']in theTEMPLATESconfig). - For development, set
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'insettings.pyto reset emails to your terminal instead of sending real emails.
That’s all! Your custom password reset flow should now work seamlessly with your templates.
内容的提问来源于stack exchange,提问作者Username

