summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Sandford <nick@sandford.id.au>2013-01-10 09:05:01 +0100
committerClaude Paroz <claude@2xlibre.net>2013-01-10 09:07:06 +0100
commitb4544dbd5bf7fec73d03f31c0e59135d64fd818c (patch)
treebe845512f82e1cef8f8f0193fd9f5d0388919c39
parentf9604c8247e1b0369fecc9855494faff3c6ae3e8 (diff)
[1.5.x] Fixed #19573 -- Allow override of username field label in AuthenticationForm
Backport of cdad0b28d from master.
-rw-r--r--django/contrib/auth/forms.py3
-rw-r--r--django/contrib/auth/tests/forms.py10
2 files changed, 11 insertions, 2 deletions
diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index 4e2f476cec..85291126b4 100644
--- a/django/contrib/auth/forms.py
+++ b/django/contrib/auth/forms.py
@@ -169,7 +169,8 @@ class AuthenticationForm(forms.Form):
# Set the label for the "username" field.
UserModel = get_user_model()
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
- self.fields['username'].label = capfirst(self.username_field.verbose_name)
+ if not self.fields['username'].label:
+ self.fields['username'].label = capfirst(self.username_field.verbose_name)
def clean(self):
username = self.cleaned_data.get('username')
diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py
index a9f894905a..543bb2001d 100644
--- a/django/contrib/auth/tests/forms.py
+++ b/django/contrib/auth/tests/forms.py
@@ -7,7 +7,7 @@ from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
ReadOnlyPasswordHashWidget)
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.core import mail
-from django.forms.fields import Field, EmailField
+from django.forms.fields import Field, EmailField, CharField
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.encoding import force_text
@@ -138,6 +138,14 @@ class AuthenticationFormTest(TestCase):
self.assertTrue(form.is_valid())
self.assertEqual(form.non_field_errors(), [])
+ def test_username_field_label(self):
+
+ class CustomAuthenticationForm(AuthenticationForm):
+ username = CharField(label="Name", max_length=75)
+
+ form = CustomAuthenticationForm()
+ self.assertEqual(form['username'].label, "Name")
+
@skipIfCustomUser
@override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))