【问题标题】:Templates render properly on deployment server but TemplateDoesNotExist error on runserver模板在部署服务器上正确呈现,但在 runserver 上出现 TemplateDoesNotExist 错误
【发布时间】:2016-10-12 13:19:39
【问题描述】:

我有这样的项目结构

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

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

【问题讨论】:

  • 尝试在您的设置中使用相对路径。 morethanseven.net/2009/02/11/…
  • 您的模板文件夹路径在开发和生产服务器中不会​​相同,正如上面所评论的,使用相对路径总是更好
  • 谢谢大家,这很有效。如果您可以将其发布为答案,我会接受它

标签: python django nginx django-templates uwsgi


【解决方案1】:

根据 xtranophilist 和 Amar 的 cmets,我使用如下相对路径并且它有效:

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

然后在模板目录中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 2021-08-24
    • 2015-05-27
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多