Django:租户的模板路径问题
Posted
技术标签:
【中文标题】Django:租户的模板路径问题【英文标题】:Django: issue with template path for tenant 【发布时间】:2021-01-08 23:41:58 【问题描述】:我有一个应用程序,根据其类别,租户可以通过/dashboard/templates/dashboard
或/dashboard2/templates/dashboard2
定向到应用程序(和模板)。
不知何故,对于dashboard2,Django 找不到该应用程序,它试图在dashboard
下找到这些模板。
这是一个dashboard2/views.py
@method_decorator(login_required, name='dispatch')
class SupplierPage2(LoginRequiredMixin,APIView):
def get(self, request, *args, **kwargs):
query = request.GET.get('search_ress', None)
print(query)
context =
#if query and request.method == 'GET':
Supplier = supplier2.objects.filter(supplier = query)
print(Supplier)
labels = Item2.objects.filter(fournisseur = query).values_list('reference', flat=True)[:10]
print(labels)
default_items = Item2.objects.filter(fournisseur = query).values_list('number_of_sales', flat=True)[:10]
print(default_items)
label1s = Item2.objects.filter(fournisseur = query).values_list('reference', flat=True)[:10]
print(label1s)
default_item1s = Item2.objects.filter(fournisseur = query).values_list('number_of_orders_placed', flat=True)[:10]
print(default_item1s)
context.update('Supplier' : Supplier, 'labels':labels, 'default_items':default_items,'label1s':label1s, 'default_item1s':default_item1s)
return render(request, 'Supplier2.html',context)
和错误:
TemplateDoesNotExist at /Supplier2.html
Supplier2.html
Request Method: GET
Request URL: https://uname.website.net/Supplier2.html
Django Version: 3.0.5
Exception Type: TemplateDoesNotExist
Exception Value:
Supplier2.html
和追溯:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /home/ubuntu/website/dashboard/templates/dashboard/dashboard2/templates/dashboard2/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/website/customers/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/exo/lib/python3.6/site-packages/django/contrib/auth/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/website/dashboard/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/website/tenantlogin/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/website/uploadfiles/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/exo/lib/python3.6/site-packages/django_tenants/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/exo/lib/python3.6/site-packages/rest_framework/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/exo/lib/python3.6/site-packages/django_tables2/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/website/dashboard2/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/exo/lib/python3.6/site-packages/django/contrib/admin/templates/Supplier2.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/ubuntu/exo/lib/python3.6/site-packages/crispy_forms/templates/Supplier2.html (Source does not exist)
当尝试指定 /dashboard2/Supplier2.html
时,我仍然在 Django 中遇到同样的错误:
TemplateDoesNotExist at /Supplier2.html
/dashboard2/Supplier2.html
这是我在 settings.py 中的模板:
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "dashboard/templates/dashboard", "dashboard2/templates/dashboard2")],
'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',
'django.template.context_processors.i18n'
],
,
,
]
在这一点上,我不知道还能尝试什么
【问题讨论】:
此模板路径已磨损/home/ubuntu/website/dashboard/templates/dashboard/dashboard2/templates/dashboard2/Supplier2.htmt
提供模板设置定义可能会有所帮助。
刚刚添加了我在 settings.py 中关于模板的内容
【参考方案1】:
修正如下设置:
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "dashboard/templates/dashboard"), os.path.join(BASE_DIR,"dashboard2/templates/dashboard2")],
'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',
'django.template.context_processors.i18n'
],
,
,
]
【讨论】:
以上是关于Django:租户的模板路径问题的主要内容,如果未能解决你的问题,请参考以下文章