Skip to main content
 首页 » 编程设计

python之不同目录中的模板

2025年05月04日10webabcd

我想这样放置项目层级

project 
 |-app 
 |   \app1 
 |      |-templates 
 |          \app1_templ.html 
 |      |- views.py 
 |      \-models.py 
 |-templates 
 |    \main.html 
 |-views.py 
 |-models.py 
 ... 

但是在 main.html 中我想使用`{%include app1_templ.html%}。假设来自 app1 的 views.py:

def main(request): 
  info= Informations.objects.all()  
  return render_to_response('main.html', {'a':info}) 

和 app1_templ.html

   {% for b in a %} 
       <li> title:{{ b.title }}</li> 
    {%empty %} 
       EMPTY 
    {% endfor %} 

我在 settings.py 的 TEMPLATE_DIRS 中添加了一个额外的文件夹信息

os.path.join(os.path.dirname(__file__), 'apps/app1/app1_templ').replace('\\','/'),  

无论如何呈现页面似乎总是给出 EMPTY。

为什么a列表好像是空的?

请您参考如下方法:

尝试传入 context_instance:

return render_to_response('main.html', {'a': info}, context_instance=RequestContext(request))