Skip to content

lucianolev/django-email-usernames

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-email-usernames

django-email-usernames is an app that takes care of letting your django site use emails for logging in, like facebook, google and other major sites.

It consists of a email authentication backend, a email login form, a login view, and also a helper view that together with the popular django-registration app makes it simple for you to let users create new accounts with email as username.

Installation & Usage

  1. First add it as any other django app, to your installed apps:

    INSTALLED_APPS = (
        ...
        'django.contrib.auth',
        'registration', #Only if using django-registration
        'email_usernames',
    )

    Obviously you also need to have the django.contrib.auth app in there, in order for authentication to be enabled.

  2. Add the EmailOrUsernameModelBackend backend to AUTHENTICATION_BACKENDS in your settings:

    AUTHENTICATION_BACKENDS = (
        # Our custom auth backend that allows email addresses to be used as
        # usernames.
        'email_usernames.backends.EmailOrUsernameModelBackend', 
        # Default auth backend that handles everything else.
        'django.contrib.auth.backends.ModelBackend', 
    )
  3. This step is important! Because the built-in django username column in the auth_user table only allows 30 character long usernames by default, you need to modify it to be at least 75 characters. If you don't do this, emails that are longer than 30 characters long will be cut off, and this app will likely not work!

    You can do this change either manually with SQL, or if you have not run syncdb for your site the first time, then django-email-usernames can try to do the change for you.

    Way #1: Changing the username column manually

    Run the following SQL:

    ALTER TABLE auth_user MODIFY COLUMN username varchar(75) NOT NULL;

    Way #2: Let django-email-usernames do it for you

    When you run python manage.py syncdb the first time, and the django.contrib.auth models are created for the first time, django-email-usernames will detect this and ask if you want to fix the username field in the auth_user table to be 75 chars long instead of 30 chars. Say yes in the prompt, and it will run the proper SQL for you. For this to work, the SQL user needs the ALTER TABLE privileges, otherwise you need to do the change manually (see way #1 above).

  4. To integrate the views, if you're using django-registration you'll need to setup the urls for login and registration, adding the following to urls.py (the order is important!):

    (r'^accounts/', include('email_usernames.backends.email.urls')),
    (r'^accounts/', include('registration.backends.default.urls')),
  5. Finally you only need to create your custom templates for login and registration like you would normally do when using django.contrib.auth & django-registration and you're done!