I am using Django for a project.

I am getting this error --> TypeError: expected str, bytes or os.PathLike object, not tuple.

It indicates that line 17 in my setting.py file. The code in line 17 is as below.

14: import os
15: # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
17: TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

Any suggestion?

2 Answers

-1
Shafikur Rahman On

TEMPLATE_DIR is a list. Change this line to

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

TEMPLATES = [
    {
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': TEMPLATE_DIR, 
     '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', 
         ], 
         }, 
     }, 
]
1
thebjorn On

If you try running this in the repl, you'll see that:

>>> BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd()))),
>>> BASE_DIR
('c:\\srv',)
>>> isinstance(BASE_DIR, tuple)
True
>>> os.path.join(BASE_DIR, 'templates')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\ntpath.py", line 84, in join
    result_path = result_path + '\\'
TypeError: can only concatenate tuple (not "str") to tuple
>>>

the problem is the , at the end of

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd()))),  
                                                                         ^
                                                                         | this one

it works if you remove it:

>>> BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd())))
>>> os.path.join(BASE_DIR, 'templates')
'c:\\srv\\templates'

in Python a comma is used to create tuples (even though many people think it's the parenthesis):

>>> 1,2,3
(1, 2, 3)

a two element tuple:

>>> 1,2
(1, 2)

and a one-element tuple:

>>> 1,
(1,)