Hello,
I have a Django application deployed on Heroku, using a Heroku Postgres database. The main site, user registration (with django-allauth ), and other features are working correctly on the live URL.
However, I cannot log in to the Django admin panel (/admin ).
The Problem:
When I create a superuser via the Heroku CLI, the command reports “Superuser created successfully.” But when I try to log in at /admin with those exact credentials, I consistently get the error:
“Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.”
What I’ve Tried:
I have confirmed the user exists in the database and have tried multiple ways to ensure it has the correct permissions, but the login always fails.
- Standard
createsuperuser:heroku run python manage.py createsuperuser(Reports success, but login fails.) - Manually Setting Permissions: I’ve confirmed via the Heroku shell that the user has the correct flags set:
# heroku run python manage.py shell
from django.contrib.auth.models import User
user = User.objects.get(username='sxc')
print(user.is_staff, user.is_superuser) # This prints True, True
# Login still fails.
- Custom Management Command: I created a custom
make_admincommand to programmatically set the flags. The command runs successfully on Heroku, but the login still fails. - Password Reset: Resetting the password via
heroku run python manage.py changepassword sxcalso succeeds but does not fix the login issue.
Key Configuration:
My settings.py includes standard production settings for Heroku:
- Database:
dj_database_urlis used to connect to the Heroku Postgres add-on. - Authentication:
django-allauthis configured as an authentication backend alongside the defaultModelBackend. - Security Middleware:
whitenoise,SessionMiddleware, andAuthenticationMiddlewareare all present and in the standard order. - HTTPS Settings:
SECURE_SSL_REDIRECT,SESSION_COOKIE_SECURE, andCSRF_COOKIE_SECUREare all set toTrue.
It feels like the user data is correct in the database, but something during the authentication or session process for the /admin login is failing specifically in the Heroku environment.
Has anyone encountered a similar issue where a superuser cannot log in to the admin panel despite having the correct credentials and database flags? Any insight would be greatly appreciated.
Thank you.