django - 配置多站点模板加载
问题描述
目前的templates
布局:
第二个站点将很快在different domain
. 两个站点的逻辑是相同的。模板的名称是相同的。需要从first domain
进入时,从 - 其模板进入时加载second domain
其模板。如果没有这样的模板,加载一些通用模板。不需要复制模板。
apps
app1
templates
folder_with_templates_1
index.html
admin
index.html
app2
templates
folder_with_templates_2
index.html
admin
index.html
templates
admin
index.html
404.html
500.html
什么是无痛的解决方案?
写点拐杖
template loader
?没有成功。如有必要,我可以列出代码。拐杖
Middleware
,它将确定网站并加载适当的模板?dirs in the settings
每个站点的配置?找到 Django-multisite,但无法配置它。如有必要,我将显示我的设置。https://github.com/ecometrica/django-multisite
第一个站点的设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '..', 'apps', 'templates/')],
'APP_DIRS': True,
'OPTIONS': {
'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',
],
},
},
]
解决方案
模板的层次结构
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,
)
推荐阅读
- laravel - 移动到新服务器后 API 失败
- dask - Dask 数据帧 read_csv 错误:无法序列化元组类型的对象
- rate-limiting - 基于同一路由的多个键的速率限制
- javascript - 小于或等于运算符:使用 NaN
- python - PyMySQL 未按预期存储数据
- javascript - React Native:如何包含第三方 JS 文件?
- python - 我想将下面的代码(神经网络)从 keras 转换为 pytorch
- sql - 如何在从 AS400 读取的 SQL Server 中修复“在 T2 类型 *N 中未找到 RIGHT”?
- ios - 下载每个图像后重新加载 tableview
- .net-assembly - 装配说明 最大尺寸