How to override admin's password change view logic?

Viewed 202

Django docs says to use path('accounts/', include('django.contrib.auth.urls')) but I don't want it to have "accounts/" url instead I only want it to remain as "admin/". However I tried overriding it as seen like so

# project urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
    path('admin/password_change/', CustomPasswordChangeView.as_view(), name='password_change'),
]
from django.contrib.auth.views import PasswordChangeView
from django.contrib.auth.forms import PasswordChangeForm

class CustomPasswordChangeView(PasswordChangeView):
    form_class = PasswordChangeForm
    success_url = reverse_lazy('password_change_done')
    template_name = 'registration/password_change_form.html'
    title = _('Password change21312312313132133')

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user
        print(kwargs['user'])
        print(self.success_url)
        return kwargs

    def form_valid(self, form):
        form.save()
        # Updating the password logs out all other sessions for the user
        # except the current one.
        update_session_auth_hash(self.request, form.user)
        return super().form_valid(form)

I tried to change the title variable value so that I would see if it worked but it doesn't seem to use the custom view that I've made. Any idea why and how to fix it?

1 Answers

URL patterns are read top down, so if you interchange your url patterns with the password_change to be first then it'll take priority and will override the default admin url if ever a duplicate exists.

Related