Django-Registration: Email as username

How do I use emails instead of username for authentication using django-registration.

Further how do I enforce this change once I have already installed the registration module.

Do I edit the solution here Anyone knows a good hack to make django-registration use emails as usernames? in the lib folder?


Dear fellow Django Coder,

I think this is the best way to do it. Good luck!

First step, is to create the form you'd like to use.


from django import forms
from registration.forms import RegistrationForm
from django.contrib.auth.models import User

class Email(forms.EmailField): 
    def clean(self, value):
        super(Email, self).clean(value)
            raise forms.ValidationError("This email is already registered. Use the 'forgot password' link on the login page")
        except User.DoesNotExist:
            return value

class UserRegistrationForm(forms.Form):
    password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
    password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
    #email will be become username
    email = Email()

    def clean_password(self):
        if['password1'] !=['password2']:
            raise forms.ValidationError('Passwords are not the same')

Here you are creating a file to override the register() function in django-registration.


from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site

from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile

from registration.backends import default

class Backend(default.DefaultBackend):
    def register(self, request, **kwargs):
        email, password = kwargs['email'], kwargs['password1']
        username = email
        if Site._meta.installed:
            site = Site.objects.get_current()
            site = RequestSite(request)
        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                    password, site)
        return new_user

Direct your urls to paths you want to use.


(r'^accounts/', include('registration.backends.default.urls')),

Tell your urls to use the custom backend for the registration view. Also, import the form you created and add it to the url to be processed by the view.


from django.conf.urls.defaults import *
from registration.backends.default.urls import *
from accounts.forms import UserRegistrationForm

urlpatterns += patterns('',

    #customize user registration form
    url(r'^register/$', 'registration.views.register',
            'backend': 'accounts.regbackend.Backend',
            'form_class' : UserRegistrationForm


Hope that works!


For Django >= 1.3 and < 1.5 there's Django email as username on github.

Recently I'm using registration and I switch easily to django-registration-email. It works fine on Django 1.4, reusing the same templates and simply adding REGISTRATION_EMAIL_ACTIVATE_SUCCESS_URL = "/profiles/create/" to redirect to the profile creation page.

Django 1.5 overcome this issue through custom User model where you can specify what field is used as username. As stated in Django 1.5 documentation:

A string describing the name of the field on the User model that is used as the unique identifier. This will usually be a username of some kind, but it can also be an email address, or any other unique identifier.

The simplest way would be to hide the username field in the registration_form template with CSS display:none property. And add a script which takes the value from the email form field and sets the value in the hidden username field. So while submitting the form, the user submits the email as username without knowing it. The only problem is since a username field, by default, cannot accept an email which is longer than 30 characters. So, for safe bet, you should increase the username length manually in your database.

Here is the new registration_form.html template:

<form method="post" action="" class="wide">
  {% csrf_token %}
    <div style="display:none">
        <label for="id_username">Username:</label>
        {% if form.username.errors %}
          <span class="error">{{ form.username.errors.as_text }}</span>
        {% endif %}
      <dd>{{ form.username }}</dd>
      <label for="id_email">Email address:</label>
      {% if %}
        <span class="error">{{ }}</span>
      {% endif %}
    <dd>{{ }}</dd>
      <label for="id_password1">Password:</label>
      {% if form.password1.errors %}
        <span class="error">{{ form.password1.errors.as_text }}</span>
      {% endif %}
    <dd>{{ form.password1 }}</dd>
      <label for="id_password2">Password (type again to catch typos):</label>
      {% if form.password2.errors %}
        <span class="error">{{ form.password2.errors.as_text }}</span>
      {% endif %}
    <dd>{{ form.password2 }}</dd>
    <dt><input type="submit" value="Sign up" /></dt>

Need Your Help

Java Time Zone is messed up

java linux timezone

I am running a Tomcat application, and I need to display some time values. Unfortunately, the time is coming up an hour off. I looked into it and discovered that my default TimeZone is being set ...

How can I detect if my process is running UAC-elevated or not?

windows-7 windows-vista uac elevation

My Vista application needs to know whether the user has launched it "as administrator" (elevated) or as a standard user (non-elevated). How can I detect that at run time?