Get another app template 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 ,

Therefore, I use this:

 return render_to_response('app_a/a.html') 

but it shows an error:

 TemplateDoesNotExist 

What do I need to do?

+4
source share
3 answers

just use render_to_response ('a.html')

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

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

Recommended format for template directories:

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

which will work with your example app_a/a.html

The reason this format is recommended is because you can safely override templates for each application.

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

+6
source

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

+1
source

In addition to the TEMPLATE_DIRS parameters, try adding django.template.loaders.app_directories.Loader to the TEMPLATE_LOADERS setting, as this will make all application templates in your INSTALLED_APPS . Thus, you do not need to put them under one master directory.

See Django Documentation for Template Loaders

+1
source

Source: https://habr.com/ru/post/1333118/


All Articles