我正在为一个项目使用 Django。
我收到这个错误 --> TypeError: expected str, bytes or os.PathLike object, not tuple.
它表示我的 setting.py 文件中的第 17 行。第 17 行的代码如下。
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')
有什么建议吗?
最佳答案
如果你尝试在 repl 中运行它,你会看到:
>>> 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
>>>
问题是
末尾的,
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd()))),
^
| this one
如果您删除它,它会起作用:
>>> BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd())))
>>> os.path.join(BASE_DIR, 'templates')
'c:\\srv\\templates'
在 Python 中,逗号用于创建元组(尽管许多人认为它是括号):
>>> 1,2,3
(1, 2, 3)
一个二元组:
>>> 1,2
(1, 2)
和一个单元素元组:
>>> 1,
(1,)
关于python - Django 设置文件中的 Django 错误 - TypeError : expected str, 字节或 os.PathLike 对象,而不是元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200330/