简体   繁体   中英

TypeError: expected str, bytes or os.PathLike object, not tuple in Django

I am getting an error TypeError: expected str, bytes or os.PathLike object, not tuple many times tried different ways from the internet nothing worked. Could anyone help me with this problem.

Here is my template part in settings.py file :

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIRS = (os.path.join(BASE_DIR / 'templates'),
                 )
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_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',
            ],
        },
    },
]

This line seems to be the problem

TEMPLATE_DIRS = (os.path.join(BASE_DIR / 'templates'),)

DIRS is looking for a list, not a list of tuples, so instead you can use

'DIRS': [os.path.join(BASE_DIR, 'templates')],

Or if you want to keep your way:

TEMPLATE_DIRS = [os.path.join(BASE_DIR / 'templates'),]

and then set DIRS to

'DIRS': TEMPLATE_DIRS,

Set template directory to:

'DIRS': [BASE_DIR / 'templates'],

If it doesn't run, set to BASE_DIR/ "templates"

TEMPLATE_DIRS = (os.path.join(BASE_DIR / 'templates'),

I think the correct statement for this is :

TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),

and then place this in TEMPLATES list as the value for DIRS

another method is simply put (os.path.join(BASE_DIR, 'templates'), on TEMPLATES list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM