mirror of https://github.com/django/django.git
Fixed #19573 -- Allow override of username field label in AuthenticationForm
This commit is contained in:
parent
62f842e2e5
commit
cdad0b28d4
|
@ -169,6 +169,7 @@ class AuthenticationForm(forms.Form):
|
||||||
# Set the label for the "username" field.
|
# Set the label for the "username" field.
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
|
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
|
||||||
|
if not self.fields['username'].label:
|
||||||
self.fields['username'].label = capfirst(self.username_field.verbose_name)
|
self.fields['username'].label = capfirst(self.username_field.verbose_name)
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
|
@ -7,7 +7,7 @@ from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
|
||||||
ReadOnlyPasswordHashWidget)
|
ReadOnlyPasswordHashWidget)
|
||||||
from django.contrib.auth.tests.utils import skipIfCustomUser
|
from django.contrib.auth.tests.utils import skipIfCustomUser
|
||||||
from django.core import mail
|
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 import TestCase
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
@ -138,6 +138,14 @@ class AuthenticationFormTest(TestCase):
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
self.assertEqual(form.non_field_errors(), [])
|
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
|
@skipIfCustomUser
|
||||||
@override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
@override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
||||||
|
|
Loading…
Reference in New Issue