{"record":{"id":"304162960bf98b38","repo":"django/django","slug":"user-s-does-not-exist","errorCode":null,"errorMessage":"user '%s' does not exist","messagePattern":"user '(.+?)' does not exist","errorType":"console","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"django/contrib/auth/management/commands/changepassword.py","lineNumber":50,"sourceCode":"        parser.add_argument(\n            \"--database\",\n            default=DEFAULT_DB_ALIAS,\n            choices=tuple(connections),\n            help='Specifies the database to use. Default is \"default\".',\n        )\n\n    def handle(self, *args, **options):\n        if options[\"username\"]:\n            username = options[\"username\"]\n        else:\n            username = getpass.getuser()\n\n        try:\n            u = UserModel._default_manager.using(options[\"database\"]).get(\n                **{UserModel.USERNAME_FIELD: username}\n            )\n        except UserModel.DoesNotExist:\n            raise CommandError(\"user '%s' does not exist\" % username)\n\n        self.stdout.write(\"Changing password for user '%s'\" % u)\n\n        MAX_TRIES = 3\n        count = 0\n        p1, p2 = 1, 2  # To make them initially mismatch.\n        password_validated = False\n        while (p1 != p2 or not password_validated) and count < MAX_TRIES:\n            p1 = self._get_pass()\n            p2 = self._get_pass(\"Password (again): \")\n            if p1 != p2:\n                self.stdout.write(\"Passwords do not match. Please try again.\")\n                count += 1\n                # Don't validate passwords that don't match.\n                continue\n            try:\n                validate_password(p2, u)\n            except ValidationError as err:","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/auth/management/commands/changepassword.py#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before:\n$ manage.py changepassword alic  # typo\nCommandError: user 'alic' does not exist\n\n// after:\n$ manage.py changepassword alice","handlingStrategy":"validation","validationCode":"from django.contrib.auth import get_user_model\nUserModel = get_user_model()\nusername = 'alice'\ndatabase = 'default'\nexists = UserModel._default_manager.using(database).filter(\n    **{UserModel.USERNAME_FIELD: username}\n).exists()\nif not exists:\n    raise SystemExit(f'user {username!r} not found on {database!r}')\n# safe to run changepassword now","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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)."],"tags":["django","management-command","changepassword","user-lookup","multi-database"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}