django 2.1.3'django.db.backends.postgis'不是可用的数据库后端

时间:2018-11-22 19:46:00

标签: django python-3.x postgis geodjango

我正在尝试将geodjango用于一个项目。在使用geodjango之前,我想尝试通过以下教程进行学习:https://docs.djangoproject.com/en/2.1/ref/contrib/gis/tutorial/

我最初安装了django 1.11,并尝试了该教程(对我来说很愚蠢)。但是,在卸载django 1.11和安装django 2.1.3之后,我遇到了一些错误。

根据教程创建模型后,我将使用命令

python3 manage.py makemigrations

我最终遇到以下错误:

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'

这是我在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',

    }
}

我最初在python2上使用django 1.11时处理了此问题,但是即使我什至使用python3也仍然会收到错误。当我运行

python3 -m django version

我得到“ 2.1.3”。我不确定为什么django 2.1.3不会以postgis作为后端,即使本教程告诉我这样做也是如此。 django安装是否有问题,例如当我运行

django-admin

它运行1.11脚本而不是2.1.3?我很迷茫,这很令人沮丧。

1 个答案:

答案 0 :(得分:3)

postgis backend位于django.contrib.gis中,而不位于django.db中:

'ENGINE': 'django.contrib.gis.db.backends.postgis',