Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
zjm1126 Avatar asked Feb 26 '23 13:02

zjm1126


2 Answers

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.

like image 118
Yuji 'Tomita' Tomita Avatar answered Feb 28 '23 01:02

Yuji 'Tomita' Tomita


You can specify a TEMPLATE_DIRS variable in settings.py, which is a tuple of directories where templates are searched for. If you add the template directory from both apps to TEMPLATE_DIRS, you should be okay (just watch out for conflicts in the search path).

like image 28
Madison Caldwell Avatar answered Feb 28 '23 01:02

Madison Caldwell