иерархия шаблонов
apps
app1
templates
site1
app1
index.html
site2
app1
index.html
app2
templates
site1
app2
index.html
site2
app2
index.html
...
templates
index.html
...
настройки
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '..', 'apps', 'templates')],
# 'APP_DIRS': True,
'OPTIONS': {
'loaders': ['apps.loaders.Loader', 'django.template.loaders.app_directories.Loader',],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# Project context processors
'apps.landing.context_processors.landing_settings',
'apps.landing.context_processors.page404_context',
'apps.landing.context_processors.user_token_data',
],
},
},
]
загрузчики
class Loader(FilesystemLoader):
def get_path_dirs(self, template_path):
return get_app_template_dirs(template_path)
def get_template_sources(self, *args, **kwargs):
start = args[0]
template_path = 'templates/site_{}/'.format(Site.objects.get_current().id)
for i in args[0].split('/'):
if '.' in i:
x = i
else:
template_path += i + '/'
if self.get_path_dirs(template_path):
template_name = self.get_path_dirs(template_path)[0] + x
else:
template_name = ''
if Site.objects.get_current().id != 1:
template_path = 'templates/site_1/'.format(Site.objects.get_current().id)
for i in args[0].split('/'):
if '.' in i:
x = i
else:
template_path += i + '/'
if self.get_path_dirs(template_path):
template_name = self.get_path_dirs(template_path)[0] + x
else:
apps_folder = os.path.dirname(os.path.abspath(__file__)) + '/templates/' + x
if os.path.exists(apps_folder):
apps_folder = os.path.dirname(os.path.abspath(__file__)) + '/templates/' + x
template_name = os.path.exists(apps_folder)
if django_version < (2, 0, 0):
args = [template_name, None]
else:
args = [template_name]
if args[0] != '':
yield Origin(
name=args[0],
template_name=start,
loader=self,
)