django/django · error · CommandError
user '%s' does not exist
Error message
user '%s' does not exist
What it means
Raised in changepassword.handle() when the user lookup `UserModel._default_manager.using(db).get(USERNAME_FIELD=username)` raises DoesNotExist. Django cannot change a password for a user that is not present in the configured database, so it aborts with a CommandError naming the offending username.
Source
Thrown at django/contrib/auth/management/commands/changepassword.py:50
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Specifies the database to use. Default is "default".',
)
def handle(self, *args, **options):
if options["username"]:
username = options["username"]
else:
username = getpass.getuser()
try:
u = UserModel._default_manager.using(options["database"]).get(
**{UserModel.USERNAME_FIELD: username}
)
except UserModel.DoesNotExist:
raise CommandError("user '%s' does not exist" % username)
self.stdout.write("Changing password for user '%s'" % u)
MAX_TRIES = 3
count = 0
p1, p2 = 1, 2 # To make them initially mismatch.
password_validated = False
while (p1 != p2 or not password_validated) and count < MAX_TRIES:
p1 = self._get_pass()
p2 = self._get_pass("Password (again): ")
if p1 != p2:
self.stdout.write("Passwords do not match. Please try again.")
count += 1
# Don't validate passwords that don't match.
continue
try:
validate_password(p2, u)
except ValidationError as err:View on GitHub (pinned to ae25a40be0)
Solutions
- Verify the user exists: `manage.py shell -c "from django.contrib.auth import get_user_model; print(get_user_model().objects.filter(username='alice').exists())"`.
- Check you are hitting the right database — pass `--database <alias>` matching where the user was created.
- Confirm the exact USERNAME_FIELD value (custom user models may use email); query by that field.
- Create the user first if it is genuinely missing.
Example fix
// before: $ manage.py changepassword alic # typo CommandError: user 'alic' does not exist // after: $ manage.py changepassword alice
Defensive patterns
Strategy: validation
Validate before calling
from django.contrib.auth import get_user_model
UserModel = get_user_model()
username = 'alice'
database = 'default'
exists = UserModel._default_manager.using(database).filter(
**{UserModel.USERNAME_FIELD: username}
).exists()
if not exists:
raise SystemExit(f'user {username!r} not found on {database!r}')
# safe to run changepassword now Prevention
- Confirm the user exists on the exact --database alias before running changepassword.
- Remember the default username is the OS login (getpass.getuser()); pass --username explicitly in automation.
- Use the correct USERNAME_FIELD for custom user models (may be email, not username).
When it happens
Trigger: Running `manage.py changepassword alice` where no user with USERNAME_FIELD == 'alice' exists in the selected database; running changepassword with no arg (defaults to getpass.getuser(), the OS login) when that OS user has no Django account; using --database to point at a shard where the user lives elsewhere.
Common situations: Typo in username; defaulting to the OS username in a container/CI where the Django user was never created; multi-database setups where the user exists only on the 'default' DB but --database points elsewhere; case-sensitivity mismatches on the USERNAME_FIELD.
Related errors
- aborted
- Aborting password change for user '%s' after %s attempts
- Session engine '%s' doesn't support clearing expired session
- 'no_color' and 'force_color' can't be used together.
- requires_system_checks must be a list or tuple.
AI-assisted analysis of django/django@ae25a40be0 (2026-08-06).
Data as JSON: /api/errors/304162960bf98b38.
Report an issue: GitHub.