可能重复:
Django 密码生成器
d = Data.objects.get(key=key)
User.objects.create_user(username = d.name, email= d.email, password = password)
如何创建随机密码并通过电子邮件将其发送给用户(d.email
)?
可能重复:
Django 密码生成器
d = Data.objects.get(key=key)
User.objects.create_user(username = d.name, email= d.email, password = password)
如何创建随机密码并通过电子邮件将其发送给用户(d.email
)?
在 djangomake_random_password
中是一个生成随机密码的内置方法
my_password = User.objects.make_random_password()
它接受参数 length
,allowd_chars
您可以限制密码长度以及特殊符号和数字
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',])
使用它来创建随机密码:
并发送电子邮件。
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'