如何创建随机密码? create_user函数

时间:2013-02-05 11:17:19

标签: django

  

可能重复:
  Django Password Generator

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

如何创建随机密码并通过电子邮件发送给用户(d.email)?

3 个答案:

答案 0 :(得分:30)

django make_random_password中的

是用于生成随机密码的内置方法

my_password = User.objects.make_random_password()

它接受参数lengthallowd_chars,您可以限制密码长度和特殊符号和数字

答案 1 :(得分:0)

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',])

答案 2 :(得分:-1)

使用此密码创建随机密码:

Random string generation with upper case letters and digits in Python

发送电子邮件。

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)

您需要在settings.py中定义一些EMAIL设置

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'someone@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'