模板在部署服务器上正确呈现,但在运行服务器上出现 TemplateDoesNotExist 错误
Templates render properly on deployment server but TemplateDoesNotExist error on runserver
我有这样的项目结构
mainproject
├── manage.py
├── mainproject
│ ├── settings.py
│ ├── templates
│ │ └── mainproject
│ │ └── index.html
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
└── app
├── admin.py
├── apps.py
├── forms.py
├── models.py
├── templates
│ └── app
│ ├── index.html
│ └── abc.html
├── urls.py
└── views.py
现在我的 settings.py 我有
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['mainproject/templates', 'app/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',
],
},
},
]
当我在部署服务器的主域上使用它时(使用 nginx 和 uwsgi 部署),它运行良好。
我可以在 domain.com
访问主项目的索引,在 domain.com/app
访问应用程序的索引
但是当使用 runserver
时,只有 domain.com:8000/app
有效,而 domain.com:8000
给出错误 TemplateDoesNotExist at /
。
为什么会这样以及如何解决这个问题?
模板加载器事后分析:
Using engine django:
django.template.loaders.filesystem.Loader: /home/mohit/mainproject/templates/mainproject/index.html (Source does not exist)
django.template.loaders.filesystem.Loader: /home/mohit/app/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/Env/mainproject/lib/python2.7/site-packages/django/contrib/admin/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/Env/mainproject/lib/python2.7/site-packages/django/contrib/auth/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/mainproject/app/templates/mainproject/index.html (Source does not exist)
如果我在 settings.py 中更改 TEMPLATES
中的 DIRS
行并使其成为
'DIRS': ['mainproject/mainproject/templates', 'app/templates'],
然后它可以在运行服务器上运行,但不能在部署服务器上运行。
根据 xtranophilist 和 Amar 的评论,我使用了如下相对路径并且有效:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
....
然后在模板目录中:
'DIRS': [BASE_DIR + '/mainproject/templates']
我有这样的项目结构
mainproject
├── manage.py
├── mainproject
│ ├── settings.py
│ ├── templates
│ │ └── mainproject
│ │ └── index.html
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
└── app
├── admin.py
├── apps.py
├── forms.py
├── models.py
├── templates
│ └── app
│ ├── index.html
│ └── abc.html
├── urls.py
└── views.py
现在我的 settings.py 我有
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['mainproject/templates', 'app/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',
],
},
},
]
当我在部署服务器的主域上使用它时(使用 nginx 和 uwsgi 部署),它运行良好。
我可以在 domain.com
访问主项目的索引,在 domain.com/app
但是当使用 runserver
时,只有 domain.com:8000/app
有效,而 domain.com:8000
给出错误 TemplateDoesNotExist at /
。
为什么会这样以及如何解决这个问题?
模板加载器事后分析:
Using engine django:
django.template.loaders.filesystem.Loader: /home/mohit/mainproject/templates/mainproject/index.html (Source does not exist)
django.template.loaders.filesystem.Loader: /home/mohit/app/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/Env/mainproject/lib/python2.7/site-packages/django/contrib/admin/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/Env/mainproject/lib/python2.7/site-packages/django/contrib/auth/templates/mainproject/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/mohit/mainproject/app/templates/mainproject/index.html (Source does not exist)
如果我在 settings.py 中更改 TEMPLATES
中的 DIRS
行并使其成为
'DIRS': ['mainproject/mainproject/templates', 'app/templates'],
然后它可以在运行服务器上运行,但不能在部署服务器上运行。
根据 xtranophilist 和 Amar 的评论,我使用了如下相对路径并且有效:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
....
然后在模板目录中:
'DIRS': [BASE_DIR + '/mainproject/templates']