Waltham WECAN
Waltham WECAN

Reputation: 491

Requested setting DATABASES, but settings are not configured error in my Django project

I am attempting to change my database settings in my django project from sqlite3 to mysql.

I edited the database object in my settings.py file :

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

I ran django-admin dbshell and got this error :

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/conf/init.py", line 39, 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.

I followed the instruction from this answer to use settings.configure()

from django.conf import settings
settings.configure()

It returned this message :

Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/conf/init.py", line 63, in configure raise RuntimeError('Settings already configured.') RuntimeError: Settings already configured.

When I ran python3 manage.py shell it gives me this error :

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 28, in raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

All I want to do is use mySql instead of sqlite db.

How do I do this ?

Upvotes: 1

Views: 1908

Answers (1)

Robert
Robert

Reputation: 3483

It seems MySQLdb packages are missing, can you check this package are installed

apt-get install mysql-server

apt-get install mysql-client

apt-get install libmysqlclient-dev

In your virtual environment,conform mysql client installed

pip install mysqlclient

then do the mysql configuration in settings.py

   DATABASES = {
      'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'test_db',#database name
      'HOST': '127.0.0.1',#
      'PORT': '3306',#mysql port
      'USER': 'root',#mysql username
      'PASSWORD': 'test123',#mysql password
       'OPTIONS': {
        'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        'charset': 'utf8mb4',
         }
     }
  }

Upvotes: 1

Related Questions