0

I connect the MySQL database to the project by Django. In the settings.py file, I prescribe the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '1',
    }
}

Next, to test the configuration, I perform the following commands:

>>> from django.db import connection
>>> cursor = connection.cursor()

The console gives me the following:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Help me, please. I recently started learning Django.

1
  • It cannot find the settings file. What is the directory structure and where are you executing the command from? Commented Sep 28, 2013 at 18:23

3 Answers 3

1

The reason you are getting the error is because you need to load the settings for django in order to use django's configuration, models and tools. django provides the shell management command which will load a Python prompt that is configured for django.

Run python manage.py shell from the directory of your project (not of your application) and then you will not get that error (you'll get a different one).

As you are learning django, don't use MySQL as your database. Use the built-in sqlite database, as suggested in the tutorial to get up to speed on django.

Sign up to request clarification or add additional context in comments.

Comments

0

Your settings.py does not have HOST and PORT.

Comments

0

If you take a look at your database configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '1',
    }
}

You have not set HOST and PORT. As your error states(not having all the required keys and their respective values will cause django to throw an error. In this case, HOST and PORT are requirements):

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

If you are developing on localhost, then your HOST should be '' and your PORT should be 3306 which is the default for MySQL (unless you have changed this during the installation).

If you are new to django, try using SQLite, since it is the easiest to get started with. But if you chose to use SQLite, then you will have to enter a pathname in NAME . Don't worry if you have not created a SQLite .db file. If none exits, it will create one for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.