zjm1126 zjm1126 - 1 month ago 8
Python Question

Get another app's templates in Django

This is the structure of my site:

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


In
views.py
, I want to get
a.html
,

So I use this :

return render_to_response('app_a/a.html')


but it shows an error :

TemplateDoesNotExist


What do I need to do?

Answer

just use render_to_response('a.html')

Assuming you have the default app directory template loaders on, the problem is that the template path is actually a.html

So in your current format, you would write a.html not app_a/a.html

The recommended format for template directories is

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

    global_templates
       app_b
            b.html

which would work with your example of app_a/a.html

The reason this format is recommended is so you can sanely override templates on a per-app basis.

You can easily get conflicting template names if all files are directly in the app template directory.