本文介绍了Django无法找到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,试图设置一个django设置模块来检查环境变量和加载设置。



我的设置模块看起来像

  / templates 
home.html

/ settings
base.py
prod.py
dev.py
test.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 ^ $,direct_to_template,{'template':'home.html'},name =home),

当我将所有的设置都放在一个文件中时,这个功能很好,但是由于我把这些文件分开,我得到错误:



/

home.html

模板加载程序postmortem

Django尝试加载这些模板,按此顺序:
使用加载器django.template.lo aders.filesystem.Loader:
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/settings/templates/home.html(文件不存在)
使用加载器django.template。 loaders.app_directories.Loader:
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/auth/templates/home.html(档案不存在)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admindocs/templates/home.html(档案不存在)
/Users/Tulsa/.Apps/ tulsa_io / lib / python2.7 / site-packages / grappelli / templates / home.html(文件不存在)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django /contrib/admin/templates/home.html(文件不存在)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/pagination/templates/home.html(File does不存在)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/djangosaml2/templates/home.html(文件不存在)
/ Users / Tulsa / Apps /塔尔萨应用-CO /塔尔萨/塔尔萨/应用/型材/模板/坎e.html(文件不存在)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/debug_toolbar/templates/home.html(文件不存在)

使用加载器django.template.loaders.eggs.Loader:

我在这里缺少什么

解决方案

将您的 PROJECT_ROOT 更改为:

  PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))

并确保您的 TEMPLATE_LOADERS 变量设置正确。



说明:



abspath base.py 的完整路径,即 /home/some-path/project-folder/settings/base.py



因此,首先 dirname 为您提供给定路径的路径名(以上获得),即, / home / some-path / project-folder / settings /



然后,第二个 dirname 给你给定路径的路径名(上面获得),即 / home / some-path / project-folder /



所以,现在当你加入这个路径到 templates 时,一切都开始工作正常。



有关更多信息,请参阅。


So ive been trying to setup a django settings module that will check the environment variable and load settings.

Heres what my settings module looks like

/templates
    home.html

/settings
    base.py
    prod.py
    dev.py
    test.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?

解决方案

Change your PROJECT_ROOT to:

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

And make sure your TEMPLATE_LOADERS variable is set properly.

Explanation:

abspath gives you the full path of the base.py, i.e., /home/some-path/project-folder/settings/base.py

Therefore, first dirname gives you the dir path name of the given path (obtained above), i.e., /home/some-path/project-folder/settings/

And then, the second dirname gives you the dir path name of the given path (obtained above), i.e., /home/some-path/project-folder/

So, now when you join this path to templates, everything starts working fine.

For more refer python docs.

这篇关于Django无法找到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 23:19