Django 配置多站点模板加载
当前Django 配置多站点模板加载,django,django-templates,multisite,Django,Django Templates,Multisite,当前模板布局: 第二个站点将很快在不同的域上创建。 两个站点的逻辑是相同的。模板的名称相同。 当从第一个域输入时,需要在从第二个域输入其模板时加载其模板。如果没有这样的模板,请加载一些常用模板。不需要复制模板 apps app1 templates folder_with_templates_1 index.html admin index.html a
模板
布局:
第二个站点将很快在不同的域上创建。
两个站点的逻辑是相同的。模板的名称相同。
当从第一个域
输入时,需要在从第二个域
输入其模板时加载其模板。如果没有这样的模板,请加载一些常用模板。不需要复制模板
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
什么是无痛解决方案选项
写一些拐杖模板加载器?没有成功。如有必要,我可以列出代码
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
Crutch中间件
,它将确定网站并加载适当的模板
在每个站点的设置中配置dirs
找到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,
)
设置
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,
)
装载机
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,
)
类加载器(文件系统加载器):
def获取路径目录(自身、模板路径):
返回获取应用模板目录(模板路径)
def get_模板_源(self、*args、**kwargs):
start=args[0]
template_path='templates/site_{}/'.format(site.objects.get_current().id)
对于参数[0]中的i。拆分(“/”):
如果i中的“.”:
x=i
其他:
模板_路径+=i+'/'
如果self.get\u path\u dirs(模板路径):
template\u name=self.get\u path\u dirs(template\u path)[0]+x
其他:
模板名称=“”
如果Site.objects.get_current().id!=1:
template_path='templates/site_1/'.format(site.objects.get_current().id)
对于参数[0]中的i。拆分(“/”):
如果i中的“.”:
x=i
其他:
模板_路径+=i+'/'
如果self.get\u path\u dirs(模板路径):
template\u name=self.get\u path\u dirs(template\u path)[0]+x
其他:
apps_folder=os.path.dirname(os.path.abspath(_文件__))+'/templates/'+x
如果os.path.存在(应用程序文件夹):
apps_folder=os.path.dirname(os.path.abspath(_文件__))+'/templates/'+x
template\u name=os.path.exists(应用程序\u文件夹)
如果django_版本<(2,0,0):
args=[模板名称,无]
其他:
args=[模板名称]
如果参数[0]!='':
产量来源(
name=args[0],
模板名称=开始,
加载器=自身,
)