如何在Windows 7上使用命令CMD从Python Django访问和配置PostgreSQL数据库

5
当我在Windows 7上运行CMD命令时:
python manage.py makemigrations

我遇到了一个错误:

django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend. Try using 'django.db.backends.XXX' where XXX is one of : 'mysql', 'oracle', 'sqlite3'

我已经安装了postgresql 10,并在我的django的setting.py中按照文档说明设置了postgresql。
我想要在Windows7上使用CMD从python django应用程序访问postgresql数据库。
我正在使用Windows 7,python 3.7.1;postgresql 10;django 2.1.5。我尝试了几次搜索解决方案,但是没有找到相关的解决方案。我已经使用pgAdmin 4 v4创建了我的postgresql数据库,但是无法使用CMD命令在Windows 7上访问它以执行诸如以下命令的操作:
python manage.py showmigrations/makemigrations/migrate 

所有的错误都与主题中提到的一样。

settings.py文件从这里开始...

"""
Django settings for my_sample project.

Generated by 'django-admin startproject' using Django 2.1.5.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret key string was here but now is purposely removed'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'my_sample.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'my_sample.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'my_sample',
        'USER': 'postgres',
        'PASSWORD': 'my_password',
        'HOST':'localhost',
        'PORT': '5432',
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

#settings.py file ends here...

#manage.py file starts here...

#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_sample.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

#manage.py file ends here...

当我运行时:
python manage.py migrate

I expect to get:

Running migrations: 
No migrations to apply. 

相反,我得到了:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend. Try using 'django.db.backends.XXX' where XXX is one of : 'mysql', 'oracle', 'sqlite3'
2个回答

0

是的,我通过“pip install psycopg2”安装了psycopg2。 - Mboera Kimambo
如果出现引擎值错误,请将错误更改为“django.db.backends.postgresql_psycopg2”,而不是“django.db.backends.postgresql”。解决方案可能在此字段中。尝试手动输入以避免编码错误。 - Simen Nielsen
我已经尝试将其删除并重新输入为“django.db.backends.postgresql_psycopg2”,但仍然出现错误:“django.core.exceptions.ImproperlyConfigured:'django.db.backends.postgresql'不是可用的数据库后端。尝试使用“django.db.backends.XXX”,其中XXX是以下之一:“mysql”、“oracle”、“sqlite3”。 - Mboera Kimambo
在语句“上述异常是以下异常的直接原因:..........”之前出现的第一个错误是“ModuleNotFoundError: No module named 'django.db.backends.postgresql'”。看起来它在检查文件“...\lib\site-packages\django\db\utils.py, line 110 in load_backend”和文件“...\lib\importlib\init.py, line 127 in import_module”后报告了这个主要错误。 - Mboera Kimambo

0
请将您的应用程序添加到以下INSTALLED_APPS列表中:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'yoursampleapp'
]

然后运行makemigrations:

python manage.py makemigrations

我已经添加了我的应用程序'my_sample',保存了setting.py文件,并从Windows 7的cmd中重新运行了python manage.py makemigrations命令,但我仍然收到相同的错误。 - Mboera Kimambo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接