Django Registration without 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. Django Registration with GMail account by btbytes 5 years, 1 month ago
  2. Testing Email Registration by osborn.steven 3 years, 2 months ago
  3. login on activation with django-registration by morgan 2 years, 2 months ago
  4. UsernameField (for clean error messages) by davepeck 2 years, 9 months ago
  5. Use email addresses for user name by chris 5 years, 2 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

#

(Forgotten your password?)