Fixed #20357 -- Allow empty username field label in `AuthentificationForm`.

This commit is contained in:
Mark Huang 2013-05-06 18:45:56 +08:00 committed by Simon Charette
parent 289afc3156
commit 0732c8e8c6
2 changed files with 22 additions and 1 deletions

View File

@ -171,7 +171,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: if self.fields['username'].label is None:
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):

View File

@ -1,6 +1,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os import os
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm, from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm, PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm,
@ -13,6 +15,7 @@ from django.test.utils import override_settings
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils._os import upath from django.utils._os import upath
from django.utils import translation from django.utils import translation
from django.utils.text import capfirst
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -146,6 +149,24 @@ class AuthenticationFormTest(TestCase):
form = CustomAuthenticationForm() form = CustomAuthenticationForm()
self.assertEqual(form['username'].label, "Name") self.assertEqual(form['username'].label, "Name")
def test_username_field_label_not_set(self):
class CustomAuthenticationForm(AuthenticationForm):
username = CharField()
form = CustomAuthenticationForm()
UserModel = get_user_model()
username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
self.assertEqual(form.fields['username'].label, capfirst(username_field.verbose_name))
def test_username_field_label_empty_string(self):
class CustomAuthenticationForm(AuthenticationForm):
username = CharField(label='')
form = CustomAuthenticationForm()
self.assertEqual(form.fields['username'].label, "")
@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',))