Hello recently I have to generate a random and unique password for each user that I have in the Model.User.
I have tried with Python to generate numbers and letters with the libraries random
and string
.
import random, string
pass = random.choice(string.ascii_uppercase + string.digits) for i in range(10)
But I think this is not efficient at all.
Django provide any solution?
Django provides solutions to the problem, researching and testing, I think the safest and most efficient is Get Random String because the string is encrypted with a sha-256 algorithm before returning it, here is an example with some code.
We can pass the length of the password we want as an argument.
And we can also pass as an argument the characters that we allow so that it makes a combination of them.
The other solution provided by Django is Make Random Password , which allows you to do the same thing, but what happens is that it does not encrypt it. Import the BaseUserManager and also accept the parameters
length
andallowed_chars
for the length and characters allowed.If we need something stronger, Django provides Make Password , which converts us from plain text to a hashed password directly.