Customize the form used by Staff user to change password

Hello,

In my project I need to set some certain fields to ltr direction because the whole project is in rtl direction so this is what I do:

class CustomUserCreationForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['email'].widget.attrs.update({'dir': 'ltr'})
        self.fields['password1'].widget.attrs.update({'dir': 'ltr'})
        self.fields['password2'].widget.attrs.update({'dir': 'ltr'})


class CustomUserChangeForm(UserChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['email'].widget.attrs.update({'dir': 'ltr'})


class CustomAdminPasswordChangeForm(AdminPasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['password1'].widget.attrs.update({'dir': 'ltr'})
        self.fields['password2'].widget.attrs.update({'dir': 'ltr'})


class CustomPasswordChangeForm(PasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['old_password'].widget.attrs.update({'dir': 'ltr'})
        self.fields['new_password1'].widget.attrs.update({'dir': 'ltr'})
        self.fields['new_password2'].widget.attrs.update({'dir': 'ltr'})

And then:

class UserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    change_password_form = CustomAdminPasswordChangeForm

The above 3 forms work as expected, however the CustomAdminPasswordChangeForm is for when the Superuser is changing the password. This form does not ask for Old Password.

But when a Staff logs into Admin site, another form is used to change the password which asks for Old Password too.

I have customized that form too (CustomPasswordChangeForm) but I can’t find how to set it in my UserAdmin class?

I have the same question now, have you find the way to solve it?

Actrually, no.

This is what I have figured so far.

I have an app called “accounts” where I have created a custom user model to use email instead of username.

Here I have a forms.py file with the following content:

from django.contrib.auth.forms import PasswordChangeForm


class CustomPasswordChangeForm(PasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['old_password'].widget.attrs.update({'dir': 'ltr'})
        self.fields['new_password1'].widget.attrs.update({'dir': 'ltr'})
        self.fleids['new_password2'].widget.attrs.update({'dir': 'ltr'})

Then in the views.py:

from django.contrib.auth.views import PasswordChangeView

from .forms import CustomPasswordChangeForm

# Create your views here.


class CustomPasswordChangeView(PasswordChangeView):
    form_class = CustomPasswordChangeForm

And in the urls.py of the app (not project):

from django.urls import path

from . import views

app_name = 'accounts'

urlpatterns = [
    path(
        'password_change/',
        views.CustomPasswordChangeView.as_view(),
        name='password_change',
    ),
]

I get no errors, but I still see the default form where all password fields are RTL aligned.

Perhaps someone here could help.

Ok I found an entirely different solution.

Create this file /templates/admin/base.html and add the following:

{% extends 'admin/base.html' %}
{% load static %}
{% block extrastyle %}
{{ block.super }}
<style>
    input[type=password], input[type=email], input[type=url], input[type=number], input[type=tel], .vTextField {
        direction: ltr;
    }
    input[type=text], .vTextField {
        direction: rtl;
    }
</style>
{% endblock %}

This assumes that you have set the template 'DIRS' to [BASE_DIR / 'templates/',] in your settings.py.