You can't just fire up Python and check things, Django doesn't know what project you want to work on. You have to do one of these things:
python manage.py shell
django-admin.py shell --settings=mysite.settings
(or whatever settings module you use)DJANGO_SETTINGS_MODULE
environment variable in your OS to mysite.settings
(This is removed in Django 1.6) Use setup_environ
in the python interpreter:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
Naturally, the first way is the easiest.
In your python shell/ipython do:
from django.conf import settings
settings.configure()
In 2017 with django 1.11.5 and python 3.6 (from the comment this also works with Python 2.7):
import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
The .py
in which you put this code should be in mysite
(the parent one)
On Django 1.9, I tried django-admin runserver
and got the same error, but when I used python manage.py runserver
I got the intended result. This may solve this error for a lot of people!
In my case, I got this when trying to run Django tests through PyCharm. I think it is because PyCharm does not load the initial Django project settings, i.e. those that manage.py shell
runs initially. One can add them to the start of the testing script or just run the tests using manage.py test
.
Versions:
in my own case in django 1.10.1 running on python2.7.11, I was trying to start the server using django-admin runserver
instead of manage.py runserver
in my project directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With