How to Solve Error Message django.core.exceptions.ImproperlyConfigured: ‘django.contrib.gis.db.backends.postgis’ isn’t an available database backend or couldn’t be imported when running Django Application

Posted on

Introduction

This article is focusing on how to solve an error message appear upon the execution of a Django application.  The error message appear upon executing a Django application.  Before going on to the detail of the error message, below is the condition of the Django application itself :

  1. First of all, there is a project with the name of ‘myproject’. The location of the project is in the following display :

    C:\repository>cd django
    
    C:\repository\django>dir
    C:\repository\django>cd myproject
     Volume in drive C is Windows-SSD 
     Volume Serial Number is CA30-19A4 
     Directory of C:\repository\django 
    
    07/22/2022 09:11 AM <DIR> . 
    07/17/2022 08:56 PM <DIR> .. 
    07/22/2022 09:09 AM <DIR> env 
    07/25/2022 04:13 PM <DIR> myproject 
                  0 File(s)  0 bytes 
                  4 Dir(s) 132,979,658,752 bytes free 
    
    C:\repository\django>
    
  2. Going on inside the project with the name of ‘myproject’, there is already an application exist within with the name of ‘djangomap’ as exist below :

    C:\repository\django>cd myproject
    C:\repository\django\myproject>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\django\myproject
    
    07/25/2022 04:13 PM <DIR> .
    07/22/2022 09:11 AM <DIR> ..
    07/25/2022 04:20 PM <DIR> djangomap
    07/22/2022 09:11 AM 687 manage.py
    07/25/2022 04:20 PM <DIR> myproject
    1 File(s) 687 bytes
    4 Dir(s) 132,968,116,224 bytes free
    
    C:\repository\django\myproject>
  3. Continue on, just get inside to the  ‘myproject’ folder. Inside the ‘myproject’ folder, there is a file with the name of ‘settings.py’ as it exist below :

    C:\repository\django\myproject\myproject> cd myproject
    C:\repository\django\myproject\myproject>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\django\myproject\myproject
    
    07/25/2022 04:20 PM <DIR> .
    07/25/2022 04:13 PM <DIR> ..
    07/22/2022 09:11 AM 411 asgi.py
    07/31/2022 02:53 PM 3,929 settings.py
    07/22/2022 09:11 AM 772 urls.py
    07/22/2022 09:11 AM 411 wsgi.py
    07/22/2022 09:11 AM 0 __init__.py
    07/31/2022 02:53 PM <DIR> __pycache__
    5 File(s) 5,523 bytes
    3 Dir(s) 132,830,490,624 bytes free
    
    C:\repository\django\myproject\myproject>
    
  4. So, the cause of the error actually exist inside in the ‘settings.py’ file. Specifically, it exist in the ‘DATABASES’ configuration setting part. The following is content of that part :
    # Database
    # https://docs.djangoproject.com/en/4.0/ref/settings/#databases
    DATABASES = {
    #    'default': {
    #        'ENGINE': 'django.db.backends.sqlite3',
    #        'NAME': BASE_DIR / 'db.sqlite3',
    #    }
        'default': {
            'ENGINE': 'django.contrib.gis.db.backends.postgis',
            'NAME': 'djangomap',
            'USER': 'postgres',
            'PASSWORD': 'password',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }
  5. Another important part for giving an additional information is that the package with the name of ‘django.contrib.gis’ is already exist. It exist as a definition for further addition as an app in the following configuration part of the ‘settings.py’ :
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.gis',
        'djangomap.apps.DjangomapConfig',
    ]

After getting the detail information about the project situation, the following is the full-length of the error message appearance upon running the Django project :

(env) C:\repository\django\myproject>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\python\python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\python\python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:\repository\django\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\repository\django\env\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:\repository\django\env\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\repository\django\env\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "C:\repository\django\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\repository\django\env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\repository\django\env\lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "C:\repository\django\env\lib\site-packages\django\apps\config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\python\python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\repository\django\env\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\repository\django\env\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\repository\django\env\lib\site-packages\django\db\models\base.py", line 141, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "C:\repository\django\env\lib\site-packages\django\db\models\base.py", line 369, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\repository\django\env\lib\site-packages\django\db\models\options.py", line 235, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "C:\repository\django\env\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "C:\repository\django\env\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "C:\repository\django\env\lib\site-packages\django\db\utils.py", line 208, in create_connection
    backend = load_backend(db["ENGINE"])
  File "C:\repository\django\env\lib\site-packages\django\db\utils.py", line 126, in load_backend
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.postgis' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

How to Solve Error Message

This part will show how to solve the problem by executing several steps in sequence. Actually there are several steps for achieving the solution. It begins by installing the necessary library which in this context, it is the GEOS library. After that, the process continue on by configuring the Django project. The configuration process is necessary in order for the Django project to know and to recognize the location of the GEOS library. So, the sequence for solving the problem exist as follows :

  1. First of all, it need the GEOS library. Well, fortunately, there is an application which is very useful for that. Actually, the application itself are bundling all the necessary libraries for to get work with maps. The application is QGIS and as a reference, the installation exist in this link. That link contains an article with the title of ‘How to Install QGIS Application in Microsoft Windows’.

  2. After installing the QGIS application, just start configure the Django application. By default in this case, the QGIS application will be available in ‘C:\OSGeo4W’. On the other hand, the necessary GEOS library exist in ‘C:\OSGeo4W\bin’. So, the following is the content of the ‘C:\OSGeo4W\bin’ :

    C:\OSGeo4W\bin>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\OSGeo4W\bin
    
    07/25/2022 04:51 PM <DIR> .
    07/25/2022 04:51 PM <DIR> ..
    07/25/2022 04:46 PM <DIR> 1033
    ...
    06/16/2022 12:08 AM 19,968 geod.exe
    06/07/2022 08:05 PM 1,897,472 geos.dll
    06/07/2022 08:06 PM 357,376 geos_c.dll
    06/16/2022 06:24 PM 33,280 geotifcp.exe
    06/16/2022 06:24 PM 160,256 geotiff.dll
    ...
    224 File(s) 336,177,144 bytes
    3 Dir(s) 132,827,037,696 bytes free
    
    (env) C:\OSGeo4W\bin>

    So, the GEOS library exist in the form of DLL file.

  3. Define it in the ‘settings.py’ file, just put in the line last line so that it will easy to recogize it as follows :

    GEOS_LIBRARY_PATH = "C:\OSGeo4W\\bin\geos_c.dll"
  4. After that, start the Django application again once more. If there is no more error appear, the Django application shall run normally without the appearance of the error messages.

Leave a Reply