[Answered ]-How to pass change_password.html to replace Django Admin Password Reset

1👍

You can use PasswordResetConfirmView and then pass the template_name parameter.

To redirect the user to the login page after resetting their password, you can set the login_url attribute to the URL of your login page.

urls.py

from django.contrib.auth import views as auth_views
from users import views as user_views

app_name = 'users'
..............
    path('login/', MyLoginView.as_view(redirect_authenticated_user=True, template_name='users/login.html'), name='login'),
    path('password/', user_views.change_password, name='change_password'),
    path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='users/change_password.html'), name='password_reset_confirm'),
    path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html', login_url='/any-login-url/'), name='password_reset_complete'),
    .............................
    ...............................

Edit

Try to create a subclass of PasswordResetConfirmView so:

# in views.py

from django.contrib.auth.views import PasswordResetConfirmView
from django.urls import reverse_lazy

class CustomPasswordResetConfirmView(PasswordResetConfirmView):
    template_name = 'users/change_password.html'
    post_reset_login = True
    success_url = reverse_lazy('any-login-url') 

With the post_reset_login attribute set to True, the user will be automatically logged in after they reset their password, and they will be redirected to the URL specified in the success_url attribute.

Then in urls.py:

from .views import CustomPasswordResetConfirmView

urlpatterns = [
    # ... 
    path('password-reset-confirm/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    # ...
]

Leave a comment