Django: TEMPLATE_DIRS vs INSTALLED_APPS

I am currently just adding an application to INSTALLED_APPS to be able to use templates from this application, but there is also a TEMPLATE_DIRS setting. When do I prefer TEMPLATE_DIRS over INSTALLED_APPS?

+3
source share
2 answers

You can use templates in TEMPLATE_DIRS to override templates coming from applications (by giving them the same name) or for templates that are related to several applications (base.html comes to mind).

This works because of the order in which templates are loaded in TEMPLATE_LOADERS (file system before app_directories).

, :

<project>/
    <app1>/templates/<app1>/
        foo.html
        bar.html
    <app2>/templates/<app2>/
        foo.html
    templates/
        <app1>/
            foo.html
        base.html
        xyzzy.html
+7

TEMPLATE_DIRS, .

, hop, , TEMPLATE_DIRS , :

import os

cwd = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = cwd[:-9]

# other code comes here ...

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, "templates"),
)
0

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


All Articles