Login

Django Registration without username

Author:
newmaniese
Posted:
April 6, 2008
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

A simple adaptation of RegistrationFormUniqueEmail from django-registration http://code.google.com/p/django-registration to allow users to register without using a username. This works great with the code from this comment: http://www.djangosnippets.org/snippets/74/#c195 to allow users to completely eliminate the need for a username.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class RegistrationFormNoUserName(RegistrationFormUniqueEmail):
    """
    A registration form that only requires the user to enter their e-mail 
    address and password. The username is automatically generated
    This class requires django-registration to extend the 
    RegistrationFormUniqueEmail
    """ 
    username = forms.CharField(widget=forms.HiddenInput, required=False)

    def clean_username(self):
        "This function is required to overwrite an inherited username clean"
        return self.cleaned_data['username']

    def clean(self):
        if not self.errors:
            self.cleaned_data['username'] = '%s%s' % (self.cleaned_data['email'].split('@',1)[0], User.objects.count())
        super(RegistrationFormNoUserName, self).clean()
        return self.cleaned_data

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

gwrtheyrn (on September 6, 2011):

I updated the username generation a little... Didn't really like the solution with the total user count.

if not self.errors:
    localpart = self.cleaned_data['email'].split('@',1)[0][:25]
    c = User.objects.filter(username=localpart).count()
    if c > 1:
        localpart += c
    self.cleaned_data['username'] = localpart

#

gwrtheyrn (on September 12, 2011):

Actually the code in my last comment was buggy. Working version:

if not self.errors:
    localpart = self.cleaned_data['email'].split('@',1)[0][:25]
    c = User.objects.filter(username=localpart).count()
    if c > 0:
        localpart += str(c + 1)
    self.cleaned_data['username'] = localpart

#

dsaracini (on June 13, 2012):

The solution above will only work the first time a duplicate is found. For example, if [email protected] registers, his username will be "joe". If [email protected] tries to register, it will find "joe" and will get a count of 1 and username will be "joe2". However, if [email protected] tries to register, the local part will be "joe". It will still get a count of 1 and it will try to set username to joe2 and your application will get an error. This is because filter is "username=joe" (thus, not finding "joe2"). A better solution, and one that will prevent this error is:

c = User.objects.filter(username__startswith=localpart).count()

#

Please login first before commenting.