Passwords are the first line of defense when it comes to securing user accounts. If you’re building a Django application, it's your responsibility to enforce good password practices so that users are protected from common attacks. In this post, I’ll walk you through how to create strong password rules in Django—rules that ensure every password includes at least:
-
One uppercase letter
-
One lowercase letter
-
One number
-
One special character (like @, #, $, etc.)
-
And has a minimum length of 8 characters
We’ll make sure to keep things simple and beginner-friendly. Let's dive in!
Why Strong Passwords Matter
Weak passwords are like using a paper lock on a steel door. Hackers often use automated tools that try thousands of password combinations in seconds. Strong passwords make it harder for these tools to succeed.
Here’s an example:
-
Weak password:
password123
-
Strong password:
A@secureP4ss
Notice how the second one has uppercase and lowercase letters, a number, and a special character. It’s not something a hacker could guess easily.
In Django, you have built-in tools to manage password security, and you can add custom rules too.
Step-by-Step: Enforcing Strong Passwords in Django
Step 1: Install Django (if you haven’t already)
If you’re new to Django, first install it using pip:
pip install django
Create a new Django project:
django-admin startproject strongpasswordsite
cd strongpasswordsite
Now, start your app:
python manage.py startapp accounts
Step 2: Add Your App to Installed Apps
In your settings.py
file, add 'accounts'
to INSTALLED_APPS
:
INSTALLED_APPS = [
...,
'accounts',
]
Step 3: Create a Custom Password Validator
Django already has built-in validators, but we want to define our own custom rule. Create a file inside your app (e.g., accounts/validators.py
) and add the following code:
import re
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
def validate_strong_password(value):
if len(value) < 8:
raise ValidationError(_('Password must be at least 8 characters long.'))
if not re.search(r'[A-Z]', value):
raise ValidationError(_('Password must contain at least one uppercase letter.'))
if not re.search(r'[a-z]', value):
raise ValidationError(_('Password must contain at least one lowercase letter.'))
if not re.search(r'[0-9]', value):
raise ValidationError(_('Password must contain at least one number.'))
if not re.search(r'[!@#$%^&*(),.?":{}|<>]', value):
raise ValidationError(_('Password must contain at least one special character.'))
This function checks every password against our rule. If a password doesn’t meet the criteria, a clear error message will be shown.
Step 4: Hook the Validator into Django Settings
Open settings.py
and find the AUTH_PASSWORD_VALIDATORS
section. Add your custom validator:
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'accounts.validators.validate_strong_password',
},
]
This tells Django to check every new password using our function.
Step 5: Test the Validation
Try creating a new user using Django’s built-in createsuperuser
command:
python manage.py createsuperuser
When prompted for a password, try entering something weak like password1
. You’ll see a message like:
Password must contain at least one uppercase letter.
Password must contain at least one special character.
Perfect! This means our validator is working correctly.
Bonus Tips for Better User Experience
-
Front-End Validation: Add JavaScript checks so users see feedback before submitting the form.
-
Password Strength Meter: Use tools like zxcvbn or custom logic to show how strong a password is in real-time.
-
Password Reset Security: Make sure password reset flows also follow the same validation rules.
-
Avoid Common Patterns: Encourage users not to use easily guessable words or patterns, like names, birthdays, or simple sequences.
Wrapping Up
Enforcing strong passwords is one of the easiest and most effective ways to protect your Django application. With just a bit of custom code, you can make sure every user follows good password hygiene.
Here’s a quick recap:
-
We created a custom password validator using Python’s
re
module. -
We connected it to Django using the
AUTH_PASSWORD_VALIDATORS
setting. -
We tested it with Django’s superuser command.
Not only does this improve security, but it also shows users that you care about their data and safety. And that’s a huge win for your app’s trustworthiness.