Created
June 4, 2012 04:02
-
-
Save jcinis/2866253 to your computer and use it in GitHub Desktop.
Generate a random username for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
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
What is xrange in this situation?