django.template.loaders.app_directories.Loader无法找到模板文件

[英]django.template.loaders.app_directories.Loader fails to find the template file


The template file is saved under the app directory, but it raises TemplateDoesNotExist exception while rendering:

模板文件保存在app目录下,但在渲染时会引发TemplateDoesNotExist异常:

Template-loader postmortem as following:

模板加载器事后监控如下:

Django tried loading these templates, in this order:

Django尝试按以下顺序加载这些模板:

Using loader django.template.loaders.app_directories.Loader:
    ...
    $PROJECT/apps/myapp/templates/search.html (File does not exist)
    ...

I'm wondering why it looks for:

我想知道它为什么寻找:

$PROJECT/apps/myapp/templates/search.html

rather than:

而不是:

$PROJECT/apps/myapp/templates/myapp/search.html

The latter does exist indeed

后者确实存在

3 个解决方案

#1


6  

$PROJECT/apps/myapp/templates/search.html. That is the path it'll look for as the doc says.

$ PROJECT /应用/ MYAPP /模板/ search.html。正如医生所说,这就是它所寻找的道路。

django.template.loaders.app_directories.Loader will look for a templates directory inside all the INSTALLED_APPS in order.

django.template.loaders.app_directories.Loader将按顺序在所有INSTALLED_APPS中查找模板目录。

#2


3  

django.template.loaders.filesystem.load_template_source: This loader loads templates from the filesystem, according to TEMPLATE_DIRS. It is enabled by default.

django.template.loaders.filesystem.load_template_source:根据TEMPLATE_DIRS,这个加载器从文件系统加载模板。它默认启用。

django.template.loaders.app_directories.load_template_source: This loader loads templates from Django applications on the filesystem. For each application in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates there.

django.template.loaders.app_directories.load_template_source:这个加载器从文件系统上的Django应用程序加载模板。对于INSTALLED_APPS中的每个应用程序,加载程序都会查找templates子目录。如果目录存在,Django会在那里查找模板。

This means you can store templates with your individual applications, making it easy to distribute Django applications with default templates. For example, if INSTALLED_APPS contains ('myproject.polls', 'myproject.music'), then get_template('foo.html') will look for templates in this order:

这意味着您可以使用各个应用程序存储模板,从而可以轻松地使用默认模板分发Django应用程序。例如,如果INSTALLED_APPS包含('myproject.polls','myproject.music'),则get_template('foo.html')将按以下顺序查找模板:

/path/to/myproject/polls/templates/foo.html
/path/to/myproject/music/templates/foo.html

Note that the loader performs an optimization when it is first imported: it caches a list of which INSTALLED_APPS packages have a templates subdirectory.

请注意,加载程序在首次导入时执行优化:它会缓存一个INSTALLED_APPS包具有templates子目录的列表。

This loader is enabled by default.

默认情况下启用此加载程序。

#3


-3  

Add them to your settings file.

将它们添加到您的设置文件中。

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

    TEMPLATE_DIRS = (
        os.path.join(PROJECT_ROOT, "/apps/myapp/templates/"),
    )

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/06/17/d46ca3edce3495c4e8670258228258c8.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号