Skip to content

Instantly share code, notes, and snippets.

@jcinis
Created June 4, 2012 04:02
Show Gist options
  • Save jcinis/2866253 to your computer and use it in GitHub Desktop.
Save jcinis/2866253 to your computer and use it in GitHub Desktop.
Generate a random username for Django
from random import choice
from string import ascii_lowercase, digits
from django.contrib.auth.models import User
def generate_random_username(length=16, chars=ascii_lowercase+digits, split=4, delimiter='-'):
username = ''.join([choice(chars) for i in xrange(length)])
if split:
username = delimiter.join([username[start:start+split] for start in range(0, len(username), split)])
try:
User.objects.get(username=username)
return generate_random_username(length=length, chars=chars, split=split, delimiter=delimiter)
except User.DoesNotExist:
return username;
@PernilleMatthews
Copy link

What is xrange in this situation?

@tredstart
Copy link

What is xrange in this situation?

xrange() used in python 2.x, in python3.x is only range()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment