为什么python manage.py runserver不起作用?

时间:2019-05-29 03:08:36

标签: django postgresql

当postgressql是后端时,命令python manage.py runserver不起作用。默认sqllite没有问题

我尝试将我的项目都保留在c和d驱动器中,但是当我使用postgressql作为后端时,在两个地方都遇到相同的错误。我还确保了postgres服务器已打开且未关闭。

在Django设置文件中,这是代码

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'postgres',
        'PASSWORD': 'abc123',
        'HOST': '127.0.0.1',
        'PORT': '5432',
     }
 }

命令提示符下错误的最后2-3行是django.db.backends.postgressql不是可用的数据库后端。尝试使用mysql,oracle,sqllite3。

详细错误

    Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\threading
.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\threading
.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[0](_exception[1]).with_traceback(_exception[2])
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\importlib
\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\db\models\base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\db\models\base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\db\models\options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length(
))
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\db\__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\db\utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Users\admin67\AppData\Local\Programs\Python\Python37-32\lib\site-pack
ages\django\db\utils.py", line 125, in load_backend
    ) from e_user
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql_psyc
opg' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'sqlite3'

1 个答案:

答案 0 :(得分:0)

为什么要使用这个:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'postgres',
        'PASSWORD': 'abc123',
        'HOST': '127.0.0.1',
        'PORT': '5432',
     }
 }

您必须这样设置:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '********',
        'HOST': 'localhost',
        'USER': '*******',
        'PASSWORD': '********'
    }
}

在名称字段中,您必须输入数据库名称 在用户中:PostgreSQL用户名 在密码中:密码用户名PostgreSQL