我使用的virtualenv,我想知道什么TEMPLATE_DIRS的settings.py应该是,例如,如果我做一个模板文件夹在我的项目文件夹的根.
emi*_*nor 17
您需要指定模板文件夹的绝对路径.始终使用正斜杠,即使在Windows上也是如此.
例如,如果项目文件夹是"/ home/djangouser/projects/myproject"(Linux)或"C:\ projects\myproject"(Windows),则TEMPLATE_DIRS如下所示:
# for Linux
TEMPLATE_DIRS = (
'/home/djangouser/projects/myproject/templates/',
)
# or for Windows; use forward slashes!
TEMPLATE_DIRS = (
'C:/projects/myproject/templates/',
)
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用指定的PROJECT_ROOT变量,并通过将其与模板文件夹的相对路径连接来生成绝对路径.这样做的好处是,如果将项目复制到其他位置,则只需更改PROJECT_ROOT.您需要导入os模块才能使其工作:
# add at the beginning of settings.py
import os
# ...
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates/'),
)
Run Code Online (Sandbox Code Playgroud)
小智 12
如果您正在使用较新版本的Django,则可能需要将其添加到TEMPLATES下的settings.py内的DIR列表中.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['[project name]/templates'], # Replace with your project name
'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',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25740 次 |
| 最近记录: |