[Django]-Django DB Settings 'Improperly Configured' Error

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 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.

πŸ‘€Pavel Anossov

41πŸ‘

In your python shell/ipython do:

from django.conf import settings

settings.configure()
πŸ‘€Montaro

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)

πŸ‘€MagTun

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!

πŸ‘€Jacob Young

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
πŸ‘€CoderGuy123

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.

πŸ‘€Dev

0πŸ‘

For people using IntelliJ, with these settings I was able to query from the shell (on windows).

enter image description here

Leave a comment