Django: 运行 'python manage.py runserver' 报错:TypeError: object of type 'WindowsPath' has no len()

8

我创建了一个新的 Django 项目,唯一的修改是在设置中将数据库从 SQLite 更改为 Postgres:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': BASE_DIR / 'yachtdrop',
        'USER': 'postgres',
        'PASSWORD': 'passsword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

我正在使用pipenv管理虚拟环境,我的pipfile如下:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "*"
requests = "*"
flask = "*"
psycopg2-binary = "*"

[dev-packages]

[requires]
python_version = "3.9"

当我输入命令'python manage.py runserver'时,系统返回'TypeError: object of type 'WindowsPath' has no len()',我不明白这是为什么。我在Windows上运行此命令。我最近重置了我的Windows系统,并重新安装了Python、Pipenv和PostgreSQL。希望有人能够帮助我解决这个问题。谢谢!
1个回答

20
Django默认情况下会在基础目录中创建db.sqllite3文件,但对于其他数据库,不需要在名称中使用“BASE_DIR”常量。它将前往主机并查找您提供的db文件名。
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'yachtdrop',
        'USER': 'postgres',
        'PASSWORD': 'passsword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

2
你真是个救星!谢谢,我为这个问题苦恼了很久。现在我明白重置我的笔记本电脑是多么荒谬了。 - Sebastian Strand
救命啊!我为了解决fixture的问题烦恼了很久。我一直使用BASE_DIR / 'fixtures'来获取绝对路径,现在只需将其替换为fixtures就可以解决问题了。 - Koushik Das

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