Django TemplateDoesNotExist in production server when it works in development

删除回忆录丶 提交于 2019-12-24 09:08:12

问题


I get the following error. It's ONLY IN PRODUCTION where I deploy Django application using apache + mod_wsgi. IT WORKS PERFECTLY IN DEVELOPMENT server(my computer):

TemplateDoesNotExist at /
base.html

postmortem. as you can see only one of my two folders from settings are being searched:

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)

Here are my settings. The base.html template is in "templates" folder.:

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

Here I confirm that this template actually exists:

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

As you can see only one folder out of two in my template dirs are being searched while in development server it works just fine. Any ideas why this could be ? Is it possible it's some kind of permissions issue.


回答1:


Adding the second entry for DIRS in settings.py fixed this issue for me.

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


来源:https://stackoverflow.com/questions/43148780/django-templatedoesnotexist-in-production-server-when-it-works-in-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!