Django模板在子目录中

时间:2013-11-02 22:26:55

标签: python django

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

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

使用类似

的目录结构
/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

1 个答案:

答案 0 :(得分: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