Как изменить сообщение по умолчанию в Django в django.contrib.auth.form

Я использовал Django default AuthenticationForm для входа в систему. Вот мой код:

from django.contrib.auth.forms import (
    AuthenticationForm,PasswordResetForm,UsernameField
)
class ProfiledAuthenticationForm(AuthenticationForm):
    username = UsernameField(
        label=_("username"),
        max_length=254,
        widget=forms.TextInput(attrs={'autofocus': True,'placeholder': 'username'}),
    )
    password = forms.CharField(
        label=_("password"),
        strip=False,
        widget=forms.PasswordInput(attrs={'placeholder': 'password'}),
    )

При неудачном входе в систему появляется стандартное оповещение. Мне нужно настроить это оповещение. Как я должен это сделать?

Вы можете изменить сообщение, перезаписав свойство error_messages в классе-наследнике.

class ProfiledAuthenticationForm(AuthenticationForm):

    error_messages = {
        'invalid_login': _("My custom error message"),
        'inactive': _("This account is inactive."),
    }

Вот мой код. В моем случае он работает хорошо.

class ProfiledAuthenticationForm(AuthenticationForm):
    username = UsernameField(
        label=_("メールアドレス"),
        max_length=254,
        widget=forms.TextInput(attrs={'autofocus': True,'placeholder': 'メールアドレス'}),
    )
    password = forms.CharField(
        label=_("パスワード"),
        strip=False,
        widget=forms.PasswordInput(attrs={'placeholder': 'パスワード'}),
    )
    #remember_me = forms.BooleanField(required=False,widget=forms.CheckboxInput(attrs={'class':'scalero-checkbox','id':'remember-me'}))
    profile_error_messages = {
        "invalid_profile": _("メールアドレスまたはパスワードが正しくありません。"
        )
    }
    error_messages = {
        'invalid_login': _(
            "ログインできません。パスワードが分からない場合、管理者に連絡してください。"            
        ),
        
    }
Вернуться на верх