类型错误:不支持 / 的操作数类型:'str' 和 'str' django setting.py

时间:2020-12-20 06:26:25

标签: django django-models django-settings

当我学习 django 时,我遇到了这个错误,我不知道如何修复它,这里是我的 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

2 个答案:

答案 0 :(得分:3)

你必须像这样删除 / 和 +

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR + 'db.sqlite3',
    }
}

答案 1 :(得分:2)

您正在学习的教程使用了 BASE_DIRpathlib.Path 对象,该对象支持用于连接路径的 / 运算符。如果使用字符串,则需要使用 pathlib 或使用 os.path.join

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}