3

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?

3
  • add setting.py TEMPLATES code Commented May 18, 2019 at 15:27
  • 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', ], }, }, ] WSGI_APPLICATION = 'mysite.wsgi.application' Commented May 18, 2019 at 15:29
  • I see you haven't voted on any answers you've gotten yet. Up-voting answers you found helpful (by clicking on the up-arrow next to an answer), and accepting answers that solved your problem (by clicking the check-mark next to the answer) makes it easy for future users to see what the correct/best solution is. It also gives points to the people who take the time to answer (and signals to future helpers that they will also get points) -- in other words, it's a win-win-win situation ;-) Commented May 18, 2019 at 15:53

3 Answers 3

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,)
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

I tried this before and now as well. Still getting the same error!
Hi Shafik, I removed the comma and issue solved even without making the TEMPLATE_DIR a directory. Thanks.
I had the same problem and searched a lot. But only this solved it finally. Plain and simple. Thanks
0

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

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.