Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't log in to admin site in Django

Tags:

python

django

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?

like image 681
Ian Campos Avatar asked Jul 03 '16 20:07

Ian Campos


3 Answers

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.

like image 130
Phanindra Purighalla Avatar answered Sep 19 '22 10:09

Phanindra Purighalla


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
like image 7
SuperNova Avatar answered Oct 20 '22 12:10

SuperNova


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.

like image 2
Godfred Owusu Avatar answered Oct 20 '22 10:10

Godfred Owusu