Reputation: 15814
In a Django form, users set their usernames. The username field in the form is defined like so:
username = forms.RegexField(max_length=50,regex=r'^[\w.@ +-]+$',
help_text=_("Required. 50 characters or fewer. Letters, digits and "
"@/./+/-/_ only."),
error_messages={
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters.")})
Validation occurs when the regular expression is matched, otherwise not. For instance, setting the username Bender*
is invalid, whereby Bender@
is valid.
I've noticed that my form throws a nasty error if the user enters arabic or persian text, instead of elegantly invalidating it. E.g. if one tries العربی
as the username, the following is thrown:
UnicodeEncodeError at /keep_username/
'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
Why doesn't this piece of text invalidate like normally (and What can I do to fix this)?
Thanks in advance!
Upvotes: 2
Views: 1197
Reputation: 4194
You also can handle it with RegexValidator
for the forms.py
from django import forms
from django.core.validators import RegexValidator
class FooBar(forms.Form):
username_validator = RegexValidator(r'^[\w.@ +-]+$',
"This value may contain only letters, "
"numbers and @/./+/-/_ characters
username = forms.CharField(validators=[username_validator])
or inside your models.py
from django.db import models
from django.core.validators import RegexValidator
class FooBar(models.Model):
username_validator = RegexValidator(r'^[\w.@ +-]+$',
"This value may contain only letters, "
"numbers and @/./+/-/_ characters
username = models.CharField(max_length=200, validators=[username_validator])
Upvotes: 1
Reputation: 53734
RegexField can take a complied regular expression as well as a string. Shall we try it this way?
import re
regex = re.compile('^[\w.@ +-]+$',re.UNICODE)
class Myclass(models.Model):
username = forms.RegexField(max_length=50,regex=regex,
help_text=_("Required. 50 characters or fewer. Letters, digits and "
"@/./+/-/_ only."),
Upvotes: 3