django generate random password

Published on and updated on
by

In this article, we will discuss how to generate a random password in Django.

In this article, we will discuss how to generate a random password in Django. We will use the Python module "random" to generate a random password. We will also use the "string" module to generate a string of random characters.

We're going to randomly generate a password and then assign it to a user in django. So, let's get started

How to generate random password in django?

First of all we're going to import User from the default django User model.

from django.contrib.auth.models import User

then,

password = User.objects.make_random_password()
password = User.objects.make_random_password(length=14)
passwrod = User.objects.make_random_password(length=14, allowed_chars="abcdefghjkmnpqrstuvwxyz01234567889")
user.set_password(password)

And that's how we can generate passwords for users in django randomly.