Django 模板不存在? [英] Django TemplateDoesNotExist?
问题描述
我的本地机器在 Ubuntu 8.10 上运行 Python 2.5 和 Nginx,Django 是从最新的开发主干构建的.
My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk.
对于我请求的每个 URL,它都会抛出:
For every URL I request, it throws:
TemplateDoesNotExist at/appname/path appname/template_name.html
TemplateDoesNotExist at /appname/path appname/template_name.html
Django 尝试按以下顺序加载这些模板:* 使用加载器 django.template.loaders.filesystem.function:* 使用加载器 django.template.loaders.app_directories.function:
Django tried loading these templates, in this order: * Using loader django.template.loaders.filesystem.function: * Using loader django.template.loaders.app_directories.function:
TEMPLATE_DIRS('/usr/lib/python2.5/site-packages/projectname/templates',)
TEMPLATE_DIRS ('/usr/lib/python2.5/site-packages/projectname/templates',)
在这种情况下,它是否在寻找 /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html?奇怪的是这个文件确实存在于磁盘上.为什么 Django 找不到它?
Is it looking for /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html in this case? The weird thing is this file does existed on disk. Why can't Django locate it?
我在 Ubuntu 9.04 上使用 Python 2.6 在远程服务器上运行相同的应用程序,没有出现此类问题.其他设置相同.
I run the same application on a remote server with Python 2.6 on Ubuntu 9.04 without such problem. Other settings are the same.
我的本地机器上是否有任何配置错误,或者可能导致我应该调查的此类错误的原因?
Is there anything misconfigured on my local machine, or what could possibly have caused such errors that I should look into?
在我的 settings.py 中,我指定了:
In my settings.py, I have specified:
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/templates/appname1/template1.html
- /usr/lib/python2.5/site-packages/projectname/templates/appname1/template2.html
- /usr/lib/python2.5/site-packages/projectname/templates/appname2/template3.html
- ...
以上所有文件都存在于磁盘上.
All the above files exist on disk.
已解决
我尝试后现在可以使用了:
It works now after I tried:
chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*
很奇怪.我不需要在远程服务器上执行此操作即可使其正常工作.
It's strange. I don't need to do this on the remote server to make it work.
推荐答案
第一个解决方案:
这些设置
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
意味着 Django 将查看您项目下 templates/
目录中的模板.
mean that Django will look at the templates from templates/
directory under your project.
假设您的 Django 项目位于 /usr/lib/python2.5/site-packages/projectname/
然后使用您的设置 django 将在 /usr/lib 下查找模板/python2.5/site-packages/projectname/templates/
Assuming your Django project is located at /usr/lib/python2.5/site-packages/projectname/
then with your settings django will look for the templates under /usr/lib/python2.5/site-packages/projectname/templates/
因此,在这种情况下,我们希望将模板移动为如下结构:
So in that case we want to move our templates to be structured like this:
/usr/lib/python2.5/site-packages/projectname/templates/template1.html
/usr/lib/python2.5/site-packages/projectname/templates/template2.html
/usr/lib/python2.5/site-packages/projectname/templates/template3.html
第二种解决方案:
如果这仍然不起作用并假设您在 settings.py 中配置了这样的应用程序:
If that still doesn't work and assuming that you have the apps configured in settings.py like this:
INSTALLED_APPS = (
'appname1',
'appname2',
'appname3',
)
默认情况下,Django 将在每个安装的应用程序下的 templates/
目录下加载模板.因此,对于您的目录结构,我们希望将我们的模板移动成这样:
By default Django will load the templates under templates/
directory under every installed apps. So with your directory structure, we want to move our templates to be like this:
/usr/lib/python2.5/site-packages/projectname/appname1/templates/template1.html
/usr/lib/python2.5/site-packages/projectname/appname2/templates/template2.html
/usr/lib/python2.5/site-packages/projectname/appname3/templates/template3.html
SETTINGS_PATH
可能没有默认定义.在这种情况下,您需要定义它(在 settings.py 中):
SETTINGS_PATH
may not be defined by default. In which case, you will want to define it (in settings.py):
import os
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
这篇关于Django 模板不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!