10

I was working through the polls tutorial and everything was fine until I tried to log in to the admin site; it just said

"Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive."

So I Googled the issue and tried everything I could. Here are all the problems I investigated:

  • Database not synced: I synced it and nothing changed.
  • No django_session table: I checked; it's there.
  • Problematic settings: The only change I made to the settings was the addition of'polls.apps.PollsConfig', to INSTALLED_APPS.
  • User not configured correctly: is_staff, is_superuser, and is_active are all True.
  • Old sessions: I checked the django_session table and it's empty.
  • Oversized message cookie: I checked and I don't even have one.
  • Created superuser while running web server: I did this, but after stopping the web server, creating a new user, and restarting the web server, and trying to log in with the new user, it still didn't work.
  • Missing or wrong URL pattern: Currently I have url(r"^admin/", admin.site.urls) in mysite/urls.py.
  • Entering wrong username: The username I created was "admin" and that's the same one I'm typing in.
  • Wrong server command: I'm using python manage.py runserver.
  • Something wrong with database: I tried deleting the database and then reapplying the migrations, but nothing changed.

Is there anything I haven't tried yet, or am I missing something?

11
  • 2
    run python manage.py createsuperuser Commented Jul 3, 2016 at 21:30
  • Cookies disabled on browser? Commented Jul 3, 2016 at 21:36
  • 1
    @joelgoldstick This is what I've been using to add users. It seems to add the superuser correctly, but I still can't log in. Commented Jul 3, 2016 at 21:43
  • 1
    Have you tried resetting the password? Commented Jul 3, 2016 at 21:51
  • 1
    @Sayse Når, ok. I didn't see that. :-) Commented Jul 4, 2016 at 9:12

6 Answers 6

8

I guess you would have created this user through other ways and not python manage.py createsuperuser

This happens when you have two authentication systems. ie.,. you might have django inbuilt authentication and (for example) DRF token based authentication.

To login to the backend(django admin) you will have to use python manage.py createsuperuser

To change the pwd, use the below,

python manage.py changepassword 
Sign up to request clarification or add additional context in comments.

2 Comments

I did use python manage.py createsuperuser, though. And why would I have two authentication systems, or need to change my password?
This is needed at least for Django 3, because the password in the database must originally pass some validation (8 characters long and not all numeric) and must be salted. See docs.djangoproject.com/en/3.0/ref/contrib/auth/….
2

Sometimes the simplest solutions can solve huge problems. For me it was enough to comment out or delete:

SESSION_COOKIE_SECURE = True 

That's all.

2 Comments

I don't recommend doing that before researching what this actually does.
This should be included for production, but if the log in issue exists in your local dev environment, temporarily commenting this out is fine. Just make sure it is active again for production.
1

Check your is_active model field. You may have set its default value to False, hence the reason it might not let you log in.

If it's like this -- is_active = models.BooleanField(default=False) -- change it to True, or inspect the database and change the value in is_active for the created superuser to 1.

Comments

1

This could also happen if the database being used is the default sqlite3 database and the settings.py has the DATABASES property referring to a db.sqlite3 file that is NOT in the same directory as manage.py is.

Comments

0

I think you should try these step:

  1. create new admin user

    python manage.py createsuperuser

  2. use new account log in admin site

  3. reset password for your account and remeber it

  4. log in admin site with your original account

Comments

0

my problem was using the correct settings module because I use different databases for local/local_proxy and production DJANGO_SETTINGS_MODULE=serverless_django.settings.local_proxy python manage.py createsuperuser worked for me

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.