【发布时间】:2020-09-11 09:46:27
【问题描述】:
在最新的 django 文档“从项目的模板目录覆盖” https://docs.djangoproject.com/en/3.1/howto/overriding-templates/ 它表明您可以将以下路径用于模板:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
...
},
]
我尝试使用[BASE_DIR / 'templates'],但我不断收到以下错误:TypeError: unsupported operand type(s) for /: 'str' and 'str'
当我将代码更改为:[BASE_DIR , 'templates'] 或 [os.path.join(BASE_DIR , 'templates')] 时,一切正常,在这种情况下没问题。
有人可以解释我在[BASE_DIR / 'templates'] 行中缺少什么吗?
谢谢。
我正在使用 Python 3.8 和 Django 3.1。
【问题讨论】:
-
使用
os.path.join()会很好,但你不应该使用[BASE_DIR , 'templates']- 即向DIRS添加两个目录 - 基本目录本身和'templates'(这可能适用于时间作为相对目录)
标签: python django django-templates python-3.8 django-3.1