views:

64

answers:

1

Hi All, Im new to django and trying to make a user registration form with few validations. Apart from this I also want a username suggestion code which will tell the user if the username he is trying to register is available or already in use. Then it should give few suggestions that might be available to choose from. Can anyone who might have worked on the same or somewhat same project help me with this.

Thanks

+1  A: 

Check out the django-registration application. And have a look at the Class registration.forms.RegistrationForm and their method clean_username.

It should be easy to extend the form to suggest some usernames.

here is some sample code to generate unique username with numbered postfixes:

    username # filled with user input or first/lastname etc.

    #check for other profile with equal names (and those with a postfix)
    others = [int(username.replace(name, "0")) 
              for p in User.objects.filter(username__startswith=username).exclude(user=self.user)
              if username.replace(name, "0").isdigit()]

    #do we need a postfix
    if len(others) > 0 and 0 in others:
        username = "%s%d" % (username, max(others) + 1)

you could fill the generated names in a Form Choice Field: http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

maersu
Hi maersu, Thanks for that info really appreciate that. I installed that app and is working with some problems which you might be able to help me with.I am stuck with extending the suggest some usernames part. can u help me what do i need to do for that
itsandy
i've update my answer. BTW: read my comment from http://stackoverflow.com/questions/2719452/django-forms-help-needed/
maersu
Hi Maersu, I added the sample code you provided to generate username with number postfixes and added it in the def clean_username(self): under all the other validations. Correct me if I'm wrong or if it needs to go somewhere else.Regards
itsandy