MaSao93
MaSao93

Reputation: 202

How to change Django default message in django.contrib.auth.form

I used Django default AuthenticationForm for login. Here is my code:

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'}),
    )

When the login is failed, an default alert is appeared. I need to customize the alert. How should I handle it?

Upvotes: 0

Views: 378

Answers (1)

Tonio
Tonio

Reputation: 1782

You can change the message overwriting the error_messages property in your inherit class.

class ProfiledAuthenticationForm(AuthenticationForm):

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

Upvotes: 2

Related Questions