Django中找不到模板(Django cant find templates)

2019-10-17 05:38发布

所以我一直都试图建立一个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(r"^$", direct_to_template, {'template' : 'home.html' }, name="home"),
)

当我有我的所有设置在一个文件中,这工作得很好,但因为我分割的文件了,我得到的错误:

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:

我缺少的是在这里吗?

Answer 1:

更改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 ,一切都开始正常工作。

欲了解更多请参考Python文档 。



Answer 2:

上述所有答案需要配置TEMPLATE_DIRS或TEMPLATE_LOADERS,这是没有必要的,正确的。 你必须把你的应用程序到INSTALLED_APPS。

例如,如果你的应用是在MyMegaApp(其中settigs.py居住),换句话说,你有一个项目结构如下图所示

MyMegaApp
   MyMegaApp
      templates
         index.html
      settings.py 
   manage.py

然后,你必须添加您的应用程序是这样

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'
)

之后,您的index.html模板将在MyMegaApp发现/模板文件夹



Answer 3:

这里的问题是,在您的设置, PROJECT_ROOT评估为它运行的目录manage.py

你可以做到这一点的TEMPLATE_DIRS设置

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

现在,追加../../相对PROJECT_ROOT 。 事情是这样的:

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))

TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, "templates"),
]


Answer 4:

你可以测试如下?

我觉得你的代码将设置PROJECT_ROOT为“/一些/路径/到/设置”

from os.path import dirname, abspath, normpath, join
PROJECT_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(PROJECT_ROOT, 'templates')),
)


Answer 5:

包括这设置

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'),

)

添加这的urls.py

from django.conf.urls.defaults import *
from  django.views.generic.simple.direct_to_template import direct_to_template
from appname.views import *

呼叫URL通过

  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'}),
  )


文章来源: Django cant find templates