Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.X] Fixed #19368 -- Ensured that login error messages adapt to ch…

…anges in the User model.

Thanks to un33k for the report.

Backport of 27f8129 from master.
  • Loading branch information...
commit 9534192c107aeaa1920408c0127920a4c61d9ca1 1 parent 311bd00
Russell Keith-Magee authored December 15, 2012
12  django/contrib/admin/forms.py
@@ -6,8 +6,8 @@
6 6
 from django.contrib.auth.forms import AuthenticationForm
7 7
 from django.utils.translation import ugettext_lazy
8 8
 
9  
-ERROR_MESSAGE = ugettext_lazy("Please enter the correct username and password "
10  
-        "for a staff account. Note that both fields are case-sensitive.")
  9
+ERROR_MESSAGE = ugettext_lazy("Please enter the correct %(username)s and password "
  10
+        "for a staff account. Note that both fields may be case-sensitive.")
11 11
 
12 12
 
13 13
 class AdminAuthenticationForm(AuthenticationForm):
@@ -26,8 +26,12 @@ def clean(self):
26 26
         if username and password:
27 27
             self.user_cache = authenticate(username=username, password=password)
28 28
             if self.user_cache is None:
29  
-                raise forms.ValidationError(message)
  29
+                raise forms.ValidationError(message % {
  30
+                    'username': self.username_field.verbose_name
  31
+                })
30 32
             elif not self.user_cache.is_active or not self.user_cache.is_staff:
31  
-                raise forms.ValidationError(message)
  33
+                raise forms.ValidationError(message % {
  34
+                    'username': self.username_field.verbose_name
  35
+                })
32 36
         self.check_for_test_cookie()
33 37
         return self.cleaned_data
12  django/contrib/auth/forms.py
@@ -148,8 +148,8 @@ class AuthenticationForm(forms.Form):
148 148
     password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
149 149
 
150 150
     error_messages = {
151  
-        'invalid_login': _("Please enter a correct username and password. "
152  
-                           "Note that both fields are case-sensitive."),
  151
+        'invalid_login': _("Please enter a correct %(username)s and password. "
  152
+                           "Note that both fields may be case-sensitive."),
153 153
         'no_cookies': _("Your Web browser doesn't appear to have cookies "
154 154
                         "enabled. Cookies are required for logging in."),
155 155
         'inactive': _("This account is inactive."),
@@ -168,8 +168,8 @@ def __init__(self, request=None, *args, **kwargs):
168 168
 
169 169
         # Set the label for the "username" field.
170 170
         UserModel = get_user_model()
171  
-        username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
172  
-        self.fields['username'].label = capfirst(username_field.verbose_name)
  171
+        self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
  172
+        self.fields['username'].label = capfirst(self.username_field.verbose_name)
173 173
 
174 174
     def clean(self):
175 175
         username = self.cleaned_data.get('username')
@@ -180,7 +180,9 @@ def clean(self):
180 180
                                            password=password)
181 181
             if self.user_cache is None:
182 182
                 raise forms.ValidationError(
183  
-                    self.error_messages['invalid_login'])
  183
+                    self.error_messages['invalid_login'] % {
  184
+                        'username': self.username_field.verbose_name
  185
+                    })
184 186
             elif not self.user_cache.is_active:
185 187
                 raise forms.ValidationError(self.error_messages['inactive'])
186 188
         self.check_for_test_cookie()

0 notes on commit 9534192

Please sign in to comment.
Something went wrong with that request. Please try again.