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
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 %}
<dl>
<div style="display:none">
<dt>
<label for="id_username">Username:</label>
{% if form.username.errors %}
<span class="error">{{ form.username.errors.as_text }}</span>
{% endif %}
</dt>
<dd>{{ form.username }}</dd>
</div>
<dt>
<label for="id_email">Email address:</label>
{% if form.email.errors %}
<span class="error">{{ form.email.errors.as_text }}</span>
{% endif %}
</dt>
<dd>{{ form.email }}</dd>
<br>
<dt>
<label for="id_password1">Password:</label>
{% if form.password1.errors %}
<span class="error">{{ form.password1.errors.as_text }}</span>
{% endif %}
</dt>
<dd>{{ form.password1 }}</dd>
<br>
<dt>
<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 %}
</dt>
<dd>{{ form.password2 }}</dd>
<br>
<dt><input type="submit" value="Sign up" /></dt>
</dl>
</form>
The Simple jQuery srcipt that sets the value in the hidden form field username is:
$(document).ready(function()
{
$('#id_email').blur(function(e)
{
var email_address = $('#id_email').val();
$('#id_username').val(email_address);
});
});
The ids id_email and id_username are set by the django-registration form automatically.
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.
project/accounts/forms.py
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)
try:
User.objects.get(email=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 self.data['password1'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password1']
Here you are creating a file to override the register() function in django-registration.
project/accounts/regbackend.py
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()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
Direct your urls to paths you want to use.
project/urls.py
(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.
project/accounts/urls.py
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
},
name='registration_register'
),
)
Hope that works!
-Matt
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.