266π
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:
- Use
python manage.py shell
- Use
django-admin.py shell --settings=mysite.settings
(or whatever settings module you use) - Set
DJANGO_SETTINGS_MODULE
environment variable in your OS tomysite.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.
41π
In your python shell/ipython do:
from django.conf import settings
settings.configure()
- [Django]-Is it bad to have my virtualenv directory inside my git repository?
- [Django]-Django TemplateDoesNotExist?
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
26π
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)
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Multiple Database Config in Django 1.2
- [Django]-How do I POST with jQuery/Ajax in Django?
12π
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!
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-Paginating the results of a Django forms POST request
- [Django]-CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
4π
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:
- Python 3.5 (in virtualenv)
- PyCharm 2016.3.2 Professional
- Django 1.10
- [Django]-Fastest way to get the first object from a queryset in django?
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-Name '_' is not defined
3π
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.
- [Django]-How can I disable logging while running unit tests in Python Django?
- [Django]-Where to put business logic in django
- [Django]-Folder Structure for Python Django-REST-framework and Angularjs
0π
- [Django]-Django-taggit β how do I display the tags related to each record
- [Django]-No handlers could be found for logger
- [Django]-Filtering dropdown values in django admin