在Django中获取另一个应用程序的模板

zjm*_*126 4 python django templates

这是我网站的结构:

mysite
    app_a
       templates
           a.html
    app_b
       templates
           b.html
       views.py
Run Code Online (Sandbox Code Playgroud)

views.py,我想得到a.html,

所以我用这个:

return render_to_response('app_a/a.html')
Run Code Online (Sandbox Code Playgroud)

但它显示错误:

TemplateDoesNotExist 
Run Code Online (Sandbox Code Playgroud)

我需要做什么?

Yuj*_*ita 6

只需使用render_to_response('a.html')

假设你有默认的app目录模板加载器,问题是模板路径实际上是 a.html

因此,在当前的格式,你会写a.html不会app_a/a.html

模板目录的推荐格式是

mysite
    app_a
       templates
           app_a
               a.html
    app_b
       templates
           app_b
               b.html
       views.py

    global_templates
       app_b
            b.html
Run Code Online (Sandbox Code Playgroud)

哪个适合你的例子 app_a/a.html

建议使用此格式的原因是您可以基于每个应用程序巧妙地覆盖模板.

如果所有文件都直接位于应用程序模板目录中,则可以轻松获取冲突的模板名称.