[Django]-Django Error in Django Setting File – TypeError: expected str, bytes or os.PathLike object, not tuple

3👍

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,)

0👍

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', 
         ], 
         }, 
     }, 
]

0👍

Use a front slash(/) instead of a comma(,) in your "TEMPLATE’s DIR:

like that: 'DIRS': [(BASE_DIR / 'templates')],

Leave a comment