[英]Django cant find templates
So ive been trying to setup a django settings module that will check the environment variable and load settings. 因此,我一直试图设置一个django设置模块,该模块将检查环境变量并加载设置。
Heres what my settings module looks like 这是我的设置模块的样子
/templates
home.html
/settings
base.py
prod.py
dev.py
test.py
base.py base.py
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, "templates"),
]
urls.py
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r"^$", direct_to_template, {'template' : 'home.html' }, name="home"),
)
When I had all of my settings in one file, this worked just fine, but since I split the files up I get the error: 当我将所有设置都放在一个文件中时,这很好,但是由于我将文件分割了,所以出现错误:
TemplateDoesNotExist at /
home.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/settings/templates/home.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/auth/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admindocs/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/grappelli/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admin/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/pagination/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/djangosaml2/templates/home.html (File does not exist)
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/apps/profiles/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/debug_toolbar/templates/home.html (File does not exist)
Using loader django.template.loaders.eggs.Loader:
what am i missing here? 我在这里想念什么?
All answers above require of configuring TEMPLATE_DIRS or TEMPLATE_LOADERS, which is not necessary and correct. 以上所有答案都需要配置TEMPLATE_DIRS或TEMPLATE_LOADERS,这是不必要且不正确的。 You just have to put YOUR application to the INSTALLED_APPS. 您只需要将您的应用程序放入INSTALLED_APPS。
For example, if your application is in the MyMegaApp (where settigs.py resides), in other words you have a project structure like below 例如,如果您的应用程序位于MyMegaApp中(settigs.py所在的位置),换句话说,您的项目结构如下
MyMegaApp
MyMegaApp
templates
index.html
settings.py
manage.py
Then you have to add your app like this 然后,您必须像这样添加您的应用
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'MyMegaApp'
)
After that your index.html template will be found in MyMegaApp/templates folder 之后,将在MyMegaApp / templates文件夹中找到index.html模板
The problem here is, in your settings, your PROJECT_ROOT
evaluates to the directory which runs manage.py
. 这里的问题是,在您的设置中,您的PROJECT_ROOT
评估为运行manage.py
的目录。
You may do this for TEMPLATE_DIRS
settings 您可以针对TEMPLATE_DIRS
设置执行此操作
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
print PROJECT_ROOT
Now, append ../../
relative to the PROJECT_ROOT
. 现在,相对于PROJECT_ROOT
附加../../
。 Something like this: 像这样:
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, "templates"),
]
Change your PROJECT_ROOT
to: 将您的PROJECT_ROOT
更改为:
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
And make sure your TEMPLATE_LOADERS
variable is set properly. 并确保正确设置了TEMPLATE_LOADERS
变量。
Explanation: 说明:
abspath
gives you the full path of the base.py
, ie, /home/some-path/project-folder/settings/base.py
abspath
提供了base.py
的完整路径,即/home/some-path/project-folder/settings/base.py
Therefore, first dirname
gives you the dir path name of the given path (obtained above), ie, /home/some-path/project-folder/settings/
因此,第一个dirname
为您提供给定路径的dir路径名(从上面获得),即/home/some-path/project-folder/settings/
And then, the second dirname
gives you the dir path name of the given path (obtained above), ie, /home/some-path/project-folder/
然后,第二个dirname
为您提供给定路径的dir路径名(从上面获得),即/home/some-path/project-folder/
So, now when you join this path to templates
, everything starts working fine. 因此,现在当您将此路径加入templates
,一切就可以正常工作了。
For more refer python docs . 有关更多信息,请参考python docs 。
can you test as below? 你可以测试如下吗?
i think your code would set PROJECT_ROOT as '/some/path/to/settings' 我认为您的代码会将PROJECT_ROOT设置为“ / some / path / to / settings”
from os.path import dirname, abspath, normpath, join
PROJECT_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(PROJECT_ROOT, 'templates')),
)
Include this in settings 在设置中包括
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SITE_ROOT, 'appname/templates'),
)
Add this to urls.py 将此添加到urls.py
from django.conf.urls.defaults import *
from django.views.generic.simple.direct_to_template import direct_to_template
from appname.views import *
call url by 呼叫网址
urlpatterns = patterns('django.views.generic.simple',
(r'^foo/$', 'direct_to_template', {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.