Full answer
A password hash must be slow, salted and one-way. Django's default hasher is PBKDF2 with SHA-256 and a high iteration count, increased in each release. Argon2 and bcrypt are available as options after installing their libraries (pip install "django[argon2]"), and the first hasher in PASSWORD_HASHERS is used for new passwords.
The stored value looks like pbkdf2_sha256$1000000$salt$hash. Because the algorithm and iteration count are inside the string, Django can verify old hashes. When a user logs in and the stored hash uses an older hasher or fewer iterations, check_password() re-hashes and saves the updated value, so hardening happens gradually without a migration.
API:
make_password("secret")returns an encoded string.make_password(None)returns an unusable password (never matches).check_password(raw, encoded)returns a boolean.user.set_password(raw)hashes but does not save; calluser.save().User.objects.create_user()hashes for you;User.objects.create(password="x")stores raw text by mistake.user.has_usable_password().
Validators in AUTH_PASSWORD_VALIDATORS: UserAttributeSimilarityValidator, MinimumLengthValidator, CommonPasswordValidator, NumericPasswordValidator. They run in forms (UserCreationForm, change-password), not on set_password(); call validate_password(raw, user) yourself in custom code.
Never log passwords, never email them, and never write your own hashing with hashlib.sha256.
import sys
import types
import django
from django.conf import settings
# Register a tiny in-memory "school" app so this example runs as one file
sys.modules["school"] = types.ModuleType("school")
sys.modules["school"].__file__ = __file__
settings.configure(
SECRET_KEY="demo-only-key-not-for-production",
ALLOWED_HOSTS=["testserver"], # the host name used by Django's test client
AUTH_PASSWORD_VALIDATORS=[
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", "OPTIONS": {"min_length": 10}},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
],
INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "school"],
DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
django.setup()
from django.contrib.auth.hashers import check_password, identify_hasher, make_password
from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError
encoded = make_password("correct horse battery")
algo, iterations, salt, digest = encoded.split("$")
print(algo, iterations.isdigit(), len(salt) > 0)
print(identify_hasher(encoded).algorithm)
print(check_password("correct horse battery", encoded), check_password("wrong", encoded))
print(make_password("correct horse battery") == encoded) # new random salt each time
unusable = make_password(None)
print(check_password("anything", unusable), unusable.startswith("!"))
for candidate in ("short", "password123", "a-long-unique-passphrase-42"):
try:
validate_password(candidate)
print(candidate, "-> ok")
except ValidationError as exc:
print(candidate, "->", exc.messages)
Standalone script. The first lines register a tiny in-memory app called school and an in-memory SQLite database so the file runs with `python file.py`. In a real project the settings live in settings.py and models live in an app's models.py. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): pbkdf2_sha256 True True pbkdf2_sha256 True False False False True short -> ['This password is too short. It must contain at least 10 characters.'] password123 -> ['This password is too common.'] a-long-unique-passphrase-42 -> ok
Key points
- Stored value is algorithm$iterations$salt$hash
- First entry in PASSWORD_HASHERS is used for new passwords
- check_password upgrades old hashes on successful login
- set_password does not save; create_user hashes
- Validators run in forms or via validate_password
Common mistakes
- Storing raw text with objects.create(password=...)
- Using hashlib.sha256 for passwords
- Forgetting user.save() after set_password()
- Assuming validators run inside set_password()
Follow-ups
- How do you migrate users from MD5 hashes to PBKDF2 safely?
- What does the salt protect against?
- Why is a high iteration count useful?
Tips
- Show the hash format
- Explain gradual upgrades
- Name the validators