模板在部署服务器上正确呈现,但在runserver上出现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 mainproject索引以及domain.com应用索引

但是当使用runserver ,只有domain.com:8000/app可以正常工作,而domain.com:8000 TemplateDoesNotExist at /给出错误的TemplateDoesNotExist at / 。

为什么这样以及如何解决这个问题?

模板加载器postmortem:

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'],

然后它在runserver上工作,并且不能在部署服务器上工作。

I have a project structure like this

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

Now in my settings.py I have

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', ], }, }, ]

When I use this on main domain on my deployment server, (deployed using nginx and uwsgi), it works all well.

I am able to access mainproject's index at domain.com and app's index at domain.com/app

But when using runserver, only domain.com:8000/app works and domain.com:8000 gives error TemplateDoesNotExist at /.

Why so and how this could be fixed?

Template loader postmortem:

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)

If I change DIRS line in TEMPLATES in settings.py and make it

'DIRS': ['mainproject/mainproject/templates', 'app/templates'],

Then it works on runserver and doesnt work on deployment server.

最满意答案

根据xtranophilist和Amar的评论,我使用相对路径如下,它的工作原理:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ....

然后在模板目录中:

'DIRS': [BASE_DIR + '/mainproject/templates']

As per comments by xtranophilist and Amar, I used relative path as follows and it worked:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ....

Then in template dirs:

'DIRS': [BASE_DIR + '/mainproject/templates']

更多推荐