Saturday, July 20, 2013

Re: create random string for username

The Django tutorial is not quite making it for me.  This is a very complicated language and all the pieces seem to not match up for me.  This I think is my fault and most of it seems to be in the use of the r hat for root and exactly how that points to views, models and dbs.  I may get there but it is not easy without someone to be a sort of guide.  Each time though I go into settings or one of the other files they get a perception of being more familiar and thus shorter.  While doing some of those I say a key set to fals for allow profanity but not sure what all that filter has passed through it.


On Sat, Jul 20, 2013 at 8:20 AM, Tim Chase <django.users@tim.thechases.com> wrote:
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.



--
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