前言:
部分模版不属于任何app,例如base.html属于所有项目共有。因此有必要在app之外创建模版目录,方法如下:
一、在settings.py中添加配置
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] , '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', ], }, }, ]这样就可以在项目目录下创建模版并使用了。
其中的 DIRS代表寻找模版的路径,注意这个路径是指除去app之外还去哪里寻找模版
注意:相同的模版名称会以找到的第一个模版为准。