0

我在 settings.py 中设置了一个目录来查看模板:

TEMPLATE_DIRS = (
    '/var/www/templates/',
)

具有类似的 dir 结构

/var/www/templates/
|__footer.html
|__header.html
|__index/
   |__index.html

我可以很好地获取页眉和页脚

>>> render_to_response('footer.html')
    <django.http.response.HttpResponse object at 0x2268d10>

但不在子目录中

>>> render_to_response('index/index.html')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/var/www/local/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 29, in render_to_response
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
  File "/var/www/local/lib/python2.7/site-packages/django/template/loader.py", line 172, in render_to_string
    return t.render(Context(dictionary))
  File "/var/www/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/var/www/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/var/www/local/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/var/www/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/var/www/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 166, in render
    template = get_template(template_name)
  File "/var/www/local/lib/python2.7/site-packages/django/template/loader.py", line 146, in get_template
    template, origin = find_template(template_name)
  File "/var/www/local/lib/python2.7/site-packages/django/template/loader.py", line 139, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist
4

1 回答 1

0

您是否检查过 TEMPLATE_LOADERS 中的 django.template.loaders.app_directories.Loader 设置,如果还没有,请尝试像这样添加它 -

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

关于模板子目录的 django 文档 -
https://docs.djangoproject.com/en/dev/ref/templates/api/#using-subdirectories

于 2013-11-02T22:46:22.957 回答