Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django 2.1.3 'django.db.backends.postgis' isn't an available database backend

I am trying to use geodjango for a project. Before I use geodjango I'm trying to do a learn it from the following tutorial: https://docs.djangoproject.com/en/2.1/ref/contrib/gis/tutorial/

I originally had django 1.11 installed and tried following that tutorial (stupid of me). However, after uninstalling django 1.11 and installing django 2.1.3 I am encountering some errors.

After I make the model according to the tutorial, I use the command

python3 manage.py makemigrations

I end up getting the following error:

Traceback (most recent call last):
  File "/home/aihoque2/.local/lib/python3.6/site- packages/django/db/utils.py", line 110, in load_backend
        return import_module('%s.base' % backend_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django.db.backends.postgis'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/db/models/base.py", line 101, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/db/models/base.py", line 305, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/db/models/options.py", line 203, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/aihoque2/.local/lib/python3.6/site-packages/django/db/utils.py", line 125, in load_backend
    ) from e_user
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgis' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

here is my configuration in 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',
    'world',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgis',
        'NAME': 'XXXX', #redacted
        'USER': 'XXXX', #redacted
        'PASSWORD': 'XXXX', #redacted
        'HOST': 'localhost',
        'PORT': '5432',

    }
}

I dealt with this problem originally when I had django 1.11 on python2, but if I even use python3 I still get the error. When I run

python3 -m django version

I get '2.1.3'. I am not sure why django 2.1.3 will not take postgis as a backend even though the tutorial tells me it does. Is there perhaps something wrong with my django installation such that when I run

django-admin

it runs the 1.11 script instead of the 2.1.3? I am quite lost and this is quite frustrating.

like image 968
Ammar Hoque Avatar asked Jan 28 '23 02:01

Ammar Hoque


1 Answers

The postgis backend is in django.contrib.gis, not django.db:

'ENGINE': 'django.contrib.gis.db.backends.postgis',
like image 113
Alasdair Avatar answered Apr 09 '23 07:04

Alasdair