【问题标题】:Django TemplateDoesNotExist in production server when it works in developmentDjango TemplateDoesNotExist 在开发中工作时在生产服务器中
【发布时间】:2017-08-26 04:53:06
【问题描述】:

我收到以下错误。仅在生产中,我使用 apache + mod_wsgi 部署 Django 应用程序。它在开发服务器(我的电脑)中完美运行:

TemplateDoesNotExist at /
base.html

验尸。如您所见,仅搜索了我的两个设置文件夹中的一个:

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/home/bot_frontend/horses/templates/base.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/base.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/base.html (File does not exist)
/home/virtualenvs/bot_frontend/lib/python2.7/site-packages/django_extensions/templates/base.html (File does not exist)

这是我的设置。 base.html 模板位于“templates”文件夹中。:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, "templates"),
            os.path.join(BASE_DIR, "horses/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',
            ],
        },
    },
]

这里我确认这个模板确实存在:

>>> from django.template.loader import get_template
>>> get_template("base.html")
<django.template.backends.django.Template object at 0x7f6af68d38d0>

正如您所见,我的模板目录中只有一个文件夹被搜索,而在开发服务器中它工作得很好。任何想法为什么会这样?是否可能是某种权限问题。

【问题讨论】:

  • Apache 将以特殊用户身份运行您的代码。目录/文件的权限是否允许其他用户访问它们。
  • @GrahamDumpleton 你能举个例子吗?
  • 运行ls -las /home/bot_frontend/horses/templates/base.htmlls -lasd /home/bot_frontend会得到什么。

标签: django apache django-templates mod-wsgi django-deployment


【解决方案1】:

在 settings.py 中为 DIRS 添加第二个条目为我解决了这个问题。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],'
        'DIRS': [BASE_DIR + "/templates", ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多