9

Possible Duplicate:
Django Password Generator

d = Data.objects.get(key=key)    
User.objects.create_user(username = d.name, email= d.email, password =  password)

How to create random password and send this via e-mail to user (d.email) ?

Community
  • 1
  • 1
mamasi
  • 915
  • 3
  • 10
  • 17

3 Answers3

35

in django make_random_password is a built in method for generating random password

my_password = User.objects.make_random_password()

it accepts parameters as length and allowd_chars with that you can limit the password length and special symbols and numbers

Ryne Everett
  • 6,427
  • 3
  • 37
  • 49
kartheek
  • 6,434
  • 3
  • 42
  • 41
  • [`make_random_password`](https://github.com/django/django/pull/15752) is deprecated in Django 4.2. Use python [`secrets`](https://docs.python.org/3/library/secrets.html#recipes-and-best-practices) instead. – Matthew Hegarty May 11 '23 at 09:55
1
def view_name(request):
    #make random password
    randompass = ''.join([choice('1234567890qwertyuiopasdfghjklzxcvbnm') for i in range(7)])

    #sending email
    message = "your message here"
    subject = "your subject here"
    send_mail(subject, message, from_email, ['to_email',])
catherine
  • 22,492
  • 12
  • 61
  • 85
-1

Use this to create random password:

Random string generation with upper case letters and digits in Python

And to send email.

from django.core.mail import send_mail
password = rand_string
send_mail('Subject here', 'Here is the password: .'+password,
               'from@example.com', ['someone@gmail.com'],
               fail_silently=False)

And you need to define some EMAIL settings in your settings.py

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'someone@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
Community
  • 1
  • 1
redDragonzz
  • 1,543
  • 2
  • 15
  • 33