One of the annoying things about Django is that it doesn’t allow logins via email out of the box if you’re using their built-in user authentication . You must create your own authentication backend in order to accomplish this. However, one of the things I love about Django is the ability to create authentication backends without too much of a headache. This comes in handy when dealing with things like allowing users to log in via Twitter, Facebook Connect, any OpenID providers, or allowing them to login via email and password.
Django authentication backends are quite simple, essentially whenever you call
the django.contrib.auth.authenticate() function, Django uses the
authenticate method in any of the classes specified by the
AUTHENTICATION_BACKENDS tuple set in your settings.py. If one of the classes
fails to authenticate the user, Django moves on to the next one until either
one successfully returns a user object or there are no other backends to
attempt, in which case it results in a failed login. A very simple
authentication backend which allows users to login via email rather than
username is the following code in backends.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
And you simply need to add to your settings.py:
1 2 3 4 | |
I generally place my backends in whatever app I create to store extra user info, but you can put it anywhere, just make sure it’s in your PYTHONPATH.