Saturday, July 20, 2013

Re: create random string for username

On 2013-07-18 20:53, Sivaram R wrote:
> While saving these forms instance,is it possible to create a
> random string for username field.How to do this,any sample would be
> great choice.

You can. Assigning random usernames isn't usually considered a very
nice thing to do to users, but assuming you have a valid use-case
for it, it's just simple Python:

import random
import string
def random_username(characters=string.ascii_lowercase, length=10):
return ''.join(
random.choice(characters)
for _ in range(length)
)

You might also want to post-process to check for profanity in it, as
you you don't (usually) want to assign profanity as a username:

BAD_WORDS = [ ... ] # all in uppercase
while True:
username = random_username()
normalized_username = random_username.upper()
for word in BAD_WORDS:
if word in normalized_username:
break
else: # note this is at the indentation level of the "for", not "if"
break
print "Username:", username

It's not a perfect check, but it will eliminate the obviously
problematic ones based on your black-list

-tkc


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate