Skip to content

Commit c801505

Browse files
committed
Fixed #5786: relaxed the validation for usernames to allow more common characters '@', etc.
This is really just a stop-gap until we come up with a improved way of handling disparate auth data, but it should help us stretch a bit more milage out of the current system. Thanks to alextreme, lbruno, and clayg. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12634 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 6476516 commit c801505

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

django/contrib/auth/forms.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class UserCreationForm(forms.ModelForm):
1111
"""
1212
A form that creates a user, with no privileges, from the given username and password.
1313
"""
14-
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$',
15-
help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
16-
error_message = _("This value must contain only letters, numbers and underscores."))
14+
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$',
15+
help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
16+
error_message = _("This value may contain only letters, numbers and @/./+/-/_ characters."))
1717
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
1818
password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput,
1919
help_text = _("Enter the same password as above, for verification."))
@@ -45,9 +45,9 @@ def save(self, commit=True):
4545
return user
4646

4747
class UserChangeForm(forms.ModelForm):
48-
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$',
49-
help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
50-
error_message = _("This value must contain only letters, numbers and underscores."))
48+
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$',
49+
help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
50+
error_message = _("This value may contain only letters, numbers and @/./+/-/_ characters."))
5151

5252
class Meta:
5353
model = User

django/contrib/auth/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class User(models.Model):
177177
178178
Username and password are required. Other fields are optional.
179179
"""
180-
username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."))
180+
username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))
181181
first_name = models.CharField(_('first name'), max_length=30, blank=True)
182182
last_name = models.CharField(_('last name'), max_length=30, blank=True)
183183
email = models.EmailField(_('e-mail address'), blank=True)

django/contrib/auth/tests/forms.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
# The username contains invalid data.
2222
2323
>>> data = {
24-
... 'username': 'jsmith@example.com',
24+
... 'username': 'jsmith!',
2525
... 'password1': 'test123',
2626
... 'password2': 'test123',
2727
... }
2828
>>> form = UserCreationForm(data)
2929
>>> form.is_valid()
3030
False
3131
>>> form["username"].errors
32-
[u'This value must contain only letters, numbers and underscores.']
32+
[u'This value may contain only letters, numbers and @/./+/-/_ characters.']
3333
3434
# The verification password is incorrect.
3535
@@ -65,15 +65,15 @@
6565
# The success case.
6666
6767
>>> data = {
68-
... 'username': 'jsmith2',
68+
... 'username': 'jsmith2@example.com',
6969
... 'password1': 'test123',
7070
... 'password2': 'test123',
7171
... }
7272
>>> form = UserCreationForm(data)
7373
>>> form.is_valid()
7474
True
7575
>>> form.save()
76-
<User: jsmith2>
76+
<User: jsmith2@example.com>
7777
7878
# The user submits an invalid username.
7979
@@ -189,7 +189,7 @@
189189
>>> form.is_valid()
190190
False
191191
>>> form['username'].errors
192-
[u'This value must contain only letters, numbers and underscores.']
192+
[u'This value may contain only letters, numbers and @/./+/-/_ characters.']
193193
194194
195195
### PasswordResetForm

docs/releases/1.2.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,10 @@ views in your :ref:`URLconf <topics-http-urls>`. This means that you can
742742
maintain complete control over the URL structure of your feeds. Like any other view, feeds views are passed a ``request`` object, so you can
743743
do anything you would normally do with a view, like user based access control,
744744
or making a feed a named URL.
745+
746+
Relaxed requirements for usernames
747+
----------------------------------
748+
749+
The built-in :class:`~django.contrib.auth.models.User` model's
750+
:attr:`~django.contrib.auth.models.User.username` field now allows a wider range
751+
of characters, including ``@``, ``+``, ``.`` and ``-`` characters.

docs/topics/auth.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Fields
7171

7272
Required. 30 characters or fewer. Alphanumeric characters only
7373
(letters, digits and underscores).
74+
75+
.. versionchanged:: 1.2
76+
Usernames may now contain ``@``, ``+``, ``.`` and ``-`` characters.
7477

7578
.. attribute:: models.User.first_name
7679

0 commit comments

Comments
 (0)