Slugify string in Django
Try this:
from django.utils.text import slugifydef return_slug(firstname, lastname): # get a slug of the firstname and last name. # it will normalize the string and add dashes for spaces # i.e. 'HaRrY POTTer' -> 'harry-potter' u_username = slugify(unicode('%s %s' % (firstname, lastname))) # split the username by the dashes, capitalize each part and re-combine # 'harry-potter' -> 'Harry-Potter' u_username = '-'.join([x.capitalize() for x in u_username.split('-')]) # count the number of users that start with the username count = User.objects.filter(username__startswith=u_username).count() if count == 0: return u_username else: return '%s-%d' % (u_username, count)