我的本地机器运行Python 2.5和Ubuntu 8.10上的Nginx, Django是根据最新的开发主干构建的。

对于我请求的每个URL,它抛出:

TemplateDoesNotExist at /appname/path appname/template_name.html Django尝试加载这些模板,顺序如下: 使用loader django.template.loaders. filessystem .function: 使用loader django.template.loaders.app_directories.function: TEMPLATE_DIRS (“/ usr / lib / python2.5 /网站/ projectname /模板”,)

在这种情况下,它是否在寻找/usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html ?奇怪的是这个文件确实存在于磁盘上。为什么Django找不到它?

我在Ubuntu 9.04上使用Python 2.6在远程服务器上运行相同的应用程序,没有出现这样的问题。其他设置相同。

在我的本地机器上是否有任何配置错误的地方,或者是什么可能导致了这样的错误,我应该调查一下?

在我的settings.py中,我指定了:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
# Find templates in the same folder as settings.py.
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)

它应该寻找以下文件:

/usr/lib/python2.5/site-packages projectname /模板/ appname1 / template1.html /usr/lib/python2.5/site-packages projectname /模板/ appname1 / template2.html /usr/lib/python2.5/site-packages projectname /模板/ appname2 / template3.html ...

以上所有文件都存在于磁盘上。

解决了

在我尝试之后,它现在工作了:

chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*

这是奇怪的。我不需要在远程服务器上执行此操作即可使其工作。


当前回答

在你的set .py文件中,用这个替换TEMPLATES数组中的DIRS

'DIRS': []

这个

'DIRS': [os.path.join(BASE_DIR, 'templates')],

但我认为你需要知道的是 你必须创建一个带有名称模板的文件夹,它应该在根路径上,否则你必须改变DIRS值

其他回答

简单的解决方案

'DIRS': [BASE_DIR, 'templates'],

这只是一种直觉,但是看看这篇关于Django模板加载的文章。特别地,确保你有django.template.loaders.app_directories。在TEMPLATE_LOADERS列表中的Loader。

对于django 1.9版本,我添加了

'DIRS': [os.path.join(BASE_DIR, 'templates')], 

一行到settings.py中的Templates块 而且效果很好

查看django试图加载模板的文件夹,在错误页面中查看template -loader postmortem,例如,错误将是这样的:

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:
django.template.loaders.filesystem.Loader: d:\projects\vcsrc\vcsrc\templates\base.html (Source does not exist)

在我的错误v证监会\ v证监会\templates\base.html不在路径。 然后将settings .py文件中的TEMPLATES更改为模板路径

TEMPLATES = [
    {    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
         # 'DIRS': [], 
        'DIRS': [os.path.join(BASE_DIR, 'vcsrc/templates')], 
        ...

找到这个元组:

    import os
    SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            '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',
                ],
            },
        },
    ]

你需要添加'DIRS'字符串

os.path.join(BASE_DIR, 'templates')

所以你需要:

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