Django TemplateDoesNotExist? [英] Django TemplateDoesNotExist?
问题描述
我的本地机器在Ubuntu 8.10上运行Python 2.5和Nginx,Django由最新的开发中继构建。
对于请求的每个URL,它抛出: p>
TemplateDoesNotExist at / appname / path
appname / template_name.html
Django尝试加载这些模板,这个顺序:
*使用loader django.template.loaders.filesystem.function:
*使用loader django.template.loaders.app_directories.function:
TEMPLATE_DIRS('/ usr /lib/python2.5/site-packages/projectname/templates',)
是否寻找 /在这种情况下,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__))
#查找与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
...
所有上述文件EXIST在磁盘上。 / p>
SOLVED
现在,我尝试了 chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname / *
很奇怪我不需要在远程服务器上执行此操作。
第一个解决方案:
此设置
TEMPLATE_DIRS =(
意味着django将查看项目下的
os.path.join(SETTINGS_PATH,'templates'),
) templates /
目录中的模板。
假设您的django项目位于 /usr/lib/python2.5/site-packages/projectname /
然后使用您的设置,django将寻找模板在 /usr/lib/python2.5/site-packages/projectname/templates /
所以在这种情况下我们要移动我们的模板,使其结构如下:
/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
第二个解决方案:
如果仍然无法运行,并假设您已在设置中配置了应用程序。 py这样:
INSTALLED_APPS =(
'appname1',
'appname2',
'appname3',
)
默认情况下,django将加载 templates /
目录下的每个安装的应用程序。所以使用你的目录结构,我们要移动我们的模板如下:
/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
希望有所帮助。 p>
SETTINGS_PATH
可能未被默认定义。在这种情况下,您需要定义它(在settings.py中):
import os
SETTINGS_PATH = os .path.dirname(os.path.dirname(__ file__))
My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk.
For every URL I request, it throws:
TemplateDoesNotExist at /appname/path appname/template_name.html 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',)
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?
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?
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'),
)
It shoule be looking for the following files
/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.
SOLVED
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.
First solution:
This settings
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
means that django will look at the templates from templates/
directory under your project.
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
Second solution:
If that still doesn't work and assuming that you have the apps configured in settings.py like this:
INSTALLED_APPS = (
'appname1',
'appname2',
'appname3',
)
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
Hope that helps.
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 TemplateDoesNotExist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!