TypeError:在最小的Django应用程序中,预期的str,字节或os.PathLike对象(而非元组)

时间:2019-05-04 20:54:02

标签: python-3.x django-2.2

我正在使用Django构建应用程序。每当我尝试在os.path.join()设置的'DIRS'列表中使用TEMPLATES时,都会得到TypeError: expected str, bytes or os.PathLike object, not tuple

BASE_DIR = os.path.dirname(os.path.abspath(__file__)),

# Settings
settings.configure(
    INSTALLED_APPS = [
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.staticfiles',
    ],
    MIDDLEWARE = [
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ],
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [
                os.path.join(BASE_DIR, 'templates'), # Here
            ],
            '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',
                ],
            },
        },
    ],
    STATIC_URL = '/static/',
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'), # ...and HERE!
    ],
)

1 个答案:

答案 0 :(得分:1)

此行:

os.path.join(BASE_DIR, 'templates'), # Here

必须更改为:

os.path.join(BASE_DIR + 'templates'), # Here

因此,现在它不再是一个元组(而是一个字符串),并且您将不再收到错误。另外,请相应地更改更多的出现次数。

还要从,中删除BASE_DIR = os.path.dirname(os.path.abspath(__file__)),,否则它将作为元组读取

因此它必须是:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))