ot8zxq
Last Updated: February 25, 2016
·
557
· tijs
Starbird crop2

Preventing failed logins from iOS users in Django

If you've ever built a reasonably sized Django website that uses the default ModelBackend authentication backend you might have run into the 'Uppercase Issue'; which happens when people sign up for your website using Uppercase@gmail.com and then later come back and can't login with uppercase@gmail.com.

These people then end up emailing you, or your client, that they cannot login. Or worse; they simply leave and don't come back.

In most cases this issue can be mitigated, if not prevented, by making sure it's harder to sign up with an uppercase email address or username by accident. The trick is to turn of the auto capitalizing 'feature' of iOS for the username field by adding the autocapitalize=off (and autocorrect=off for good measure) attributes.

Since your probably using the default AuthenticationForm you will have to override the default attributes for the input widget. But this is very simple!

class LoginForm(AuthenticationForm):
    """ Subclass the default AuthenticationForm and overwrite the username widget attributes """

    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs.update(
            {'autocapitalize':'off', 'autocorrect': 'off'})

That's all there is to it! Even when your using email addresses as the login 'username' this will work as long as the field is still called username.

You can also take this one step further and simply allow user lookups to be case independent. Check out this ultra simple fix for the ModelBackend to take care of that: https://gist.github.com/tijs/7988341

Say Thanks
Respond