Can't sign in to Django admin

2019-04-06 19:16发布

I can't sign in to Django admin with any of the superusers I've created. Tried creating new superusers, changing passwords, etc. - no error messages for any of these processes, but still can't sign in.

I'm not sure if it's related, but I also can't run django-admin.py commands. Here is what I've done so far:

$ django-admin.py validate  
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.  
$ export DJANGO_SETTINGS_MODULE=mysite.settings  
$ django-admin.py validate  
Error: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings

sys.path shows ['/Users/joerobinson/Sites/django/mysite' ... (other stuff on path)]

Is there something else I need to do to register the mysite module?

python manage.py runserver works fine.

Conclusion
For the question about logging in to django admin, I did not have django.contrib.auth.backends.ModelBackend included in my AUTHENTICATION_BACKENDS - adding this allowed me to sign in to admin.

I'm still working on the django-admin.py configuration question (which appears to not be related), and will reopen it in a separate question.

3条回答
可以哭但决不认输i
2楼-- · 2019-04-06 19:57

I ran into a similair problem but, I couldn't login on the admin interface even after removing the database and creating it from scratch. The problem was caused by a real silly mistake.

Don't run python manage.py testserver by accident. Make sure you use python manage.py runserver.

查看更多
你好瞎i
3楼-- · 2019-04-06 20:00

Well, to answer one of your questions, the parent folder of mysite is what needs to be on the python path. Adding mysite itself to your python path will expose the contents of mysite, not the mysite module to python.

As for runserver working / passwords not working, I haven't a clue. Have you customized anything with authentication?

Can you start the shell and try authenticating with your superuser? http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate

You said in a comment that "is_staff was set appropriately... same problem." -- does that mean you were able to log in with the first superuser to set is_staff??

查看更多
放我归山
4楼-- · 2019-04-06 20:01

In my case, I had "SESSION_COOKIE_SECURE = True" because I was trying SSL configurations. Just commenting this line worked.

查看更多
登录 后发表回答