First time here? Check out the FAQ!
3

How to make usernames not case sensitive?
 

Is it possible to make user names not case sensitive?

It may cause confusion, if there are users: Toms, toms, tOms, toMs, TomS, TOms, ...

Furthermore user are sometimes confused, when they are trying to log in and they forget that their username starts with a capital letter.

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
Toms's avatar
767
Toms
asked 11 years ago

Comments

Interesting to have an answer for this issue

Mustafa's avatar Mustafa (11 years ago)
see more comments

1 Answer

0

Taken from here: shopfiber.com/case-insensitive-username-login-in-django/
Add custom proxy to model authentication backend:

class CaseInsensitiveModelBackend(ModelBackend):
    """
    By default ModelBackend does case _sensitive_ username authentication, which isn't what is
    generally expected.  This backend supports case insensitive username authentication.
    """
    def authenticate(self, username=None, password=None):
        try:
            user = User.objects.get(username__iexact=username)
            if user.check_password(password):
                return user
            else:
                return None
        except User.DoesNotExist:
            return None

Once you have the model defined, edit your settings.py and specify “AUTHENTICATION_BACKENDS”.

AUTHENTICATION_BACKENDS = ('myproject.myapp.backends.CaseInsensitiveModelBacke

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
wdmytriy's avatar
13
wdmytriy
answered 11 years ago
link

Comments

see more comments