Which is a better alternative to the Django make_random_password Deprecated function?

170 Views Asked by At

This aswer suggests the use of the make_random_password, but it is deprecated since Django 4.2. I assume there is a good reason for eliminating this fuction, so what should I use instead?

I searched for alternatives, but could not find any native to Django. I can create my own solution using hashlib, but should I?

1

There are 1 best solutions below

1
Dong On BEST ANSWER

It’s very easy to implement this function.

import secrets 

def make_random_password(length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
    return ''.join(secrets.choice(allowed_chars) for i in range(length))