0

this is my forms.py

regex = re.compile(ur'  range ?? ',  re.UNICODE)

class EditProfileForm(UserChangeForm):
    username = forms.RegexField(max_length=50, regex=regex,)
    class Meta():
        model = User
        fields =  ('username','email','first_name','last_name','password')

if there is a way to allow arabic character in the field

  • 1
    What about `\p{Arabic}`? – ctwheels Sep 08 '17 at 15:55
  • form validation raise : Enter a valid value. when regex = re.compile(ur'\p{Arabic}',re.UNICODE) –  Sep 08 '17 at 16:13
  • 1
    Take a look at this post: [Perl Compatible Regular Expression (PCRE) in Python](https://stackoverflow.com/questions/7063420/perl-compatible-regular-expression-pcre-in-python) it does a good job of explaining your issue and gives a workaround for python – ctwheels Sep 08 '17 at 16:32

1 Answers1

0

this is the solution:

def clean_username(self):
        username = self.cleaned_data.get('username')
        match = re.compile(ur'^[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FFa-zA-Z]+[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FFa-zA-Z-_]*$')
        if match.findall(username):
            return username

But: the validator from django.contrib.auth.models:

username_validator = UnicodeUsernameValidator() if six.PY3 else ASCIIUsernameValidator()

    username = models.CharField(
        _('username'),
        max_length=150,
        unique=True,
        help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        validators=[username_validator],
        error_messages={
            'unique': _("A user with that username already exists."),
        },

stopped me so i delete it :p