How to quickly encrypt a password string in Django without an User Model?

There is a small util function just for that: make_password.


An update on this question since the previous answer does not seem to be supported.

import crypt
# To encrypt the password. This creates a password hash with a random salt.
password_hash = crypt.crypt(password)

# To check the password.
valid_password = crypt.crypt(cleartext, password_hash) == password_hash

Source: https://docs.python.org/2/library/crypt.html


You can make use of Django auth hashers:

from django.contrib.auth.hashers import make_password

password = make_password('somepass@123')

The version of Django should be 1.8 and above. I have tested in the latest version Django 3+

Tags:

Python

Django