Search code examples
djangodjango-formsdjango-users

username field in django sign up form


I use from django.contrib.auth.forms import UserCreationForm in forms.py for creating sign up form. first I register with a username, for example ali and again when I try to register with Ali it allows me to create user. how can I prevent registering with two same username like Ali and ali ?


Solution

  • This worked for me:

    def clean_username(self):
        username = self.cleaned_data['username']
        username1=str(username)
        username1=username.lower()
        if User.objects.filter(username=username1).exists():
            self._errors["username"] = ErrorList([u"username exists"])