And*_*rea 4 python django django-templates python-3.8 django-3.1
在最新的 django 文档“从项目\xe2\x80\x99s 模板目录覆盖”\n https://docs.djangoproject.com/en/3.1/howto/overriding-templates/ \nit 中显示您可以使用以下内容模板路径:
\nTEMPLATES = [\n {\n \'BACKEND\': \'django.template.backends.django.DjangoTemplates\',\n \'DIRS\': [BASE_DIR / \'templates\'],\n \'APP_DIRS\': True,\n ...\n },\n]\n
Run Code Online (Sandbox Code Playgroud)\n我尝试使用[BASE_DIR / \'templates\']
但不断收到以下错误:
\nTypeError: unsupported operand type(s) for /: \'str\' and \'str\'
[BASE_DIR , \'templates\']
当我将代码更改为:或 时,一切正常[os.path.join(BASE_DIR , \'templates\')]
,在这种情况下没有问题。
\n有人可以解释一下我在这条线上缺少什么吗[BASE_DIR / \'templates\']
?\n谢谢。
我正在使用 Python 3.8 和 Django 3.1。
\n为了使用BASE_DIR / 'templates'
,您需要BASE_DIR
成为Path()
.
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
Run Code Online (Sandbox Code Playgroud)
我怀疑你的settings.py是用早期版本的Django创建的,因此BASE_DIR
是一个字符串,例如
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Run Code Online (Sandbox Code Playgroud)