I have an app where depending on its category, a tenant is either directed to the app (and templates) at /dashboard/templates/dashboard or /dashboard2/templates/dashboard2.
somehow, for dashboard2, the app is not found by Django and it tries to find those templates under dashboard.
here is a 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)
and the error:
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
and the traceback:
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)
when try specifying /dashboard2/Supplier2.html I still get the same error in Django:
TemplateDoesNotExist at /Supplier2.html
/dashboard2/Supplier2.html
here is what I have in settings.py for templates:
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'
],
},
},
]
at this point, I don't know what else to try
Fix the settings as below:
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'
],
},
},
]
Related
My project structure is roughly as follows:
dinnerproject/
dinnerproject/
settings.py
dinners/
templates/dinners/
main.html
templates/
base.html
manage.py
In settings.py I've got TEMPLATE configured like so:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
And dinners app is added to INSTALLED_APPS.
I use a TemplateView with template_name = "main.html", which extends base.html.
But when I try to open the page that's supposed to return main.html, I keep getting a TemplateDoesNotExist error saying:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\User\dinnerproject\dinnerproject\templates\main.html (Source does not exist)
(...)
django.template.loaders.app_directories.Loader: C:\Users\User\dinnerproject\dinnerproject\dinners\templates\main.html (Source does not exist)
For some reason, my project name is duplicated in the paths, so django cannot find the right directories. What am I doing wrong?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'dinnerproject','templates'),
os.path.join(BASE_DIR,'dinners','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',
],
},
},
]
urls.py:
app_name="your app name"
This will solve your error
I have a problem with my Django project : the Templates engines search at the good path but they don't find it... I tried everything :/
I think my settings.py file is ok :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'public',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
and here my view code :
from django.shortcuts import render
def enregistrement(request):
return render(request, 'public/register.html')
I put my register.html in C:\SitePetitDejs\public\templates\public (yes the path is strange but it symplifies the problem)
And, when I go on http://127.0.0.1:8000/public/register I have "TemplateDoesNotExist at /public/register" error
The strangest thing is that the engines search at the good path :
django.template.loaders.filesystem.Loader: C:\SitePetitDejs\templates\public\register.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\templates\public\register.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\templates\public\register.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\SitePetitDejs\public\templates\public\register.html (Source does not exist)
Very blur behavior from the engines...
Maybe they don't have the permissions ? (The file is readable...)
I don't know how to solve this problem, I hope will be able to help me :)
Thankss
Is it possible to set the django project template loading priority so that first of all it loads the apps' "templates" folder and after that the project "templates". If the template exists in the app folder, then use it. If it does not exist in the app folder, then try load from project folder.
Or it is not normal way to load templates?
I ask because I see in the exception, that Django tries to load first of all global templates:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: D:\myprojects\my-website\src\templates\home.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\User\Python27\lib\site-packages\django\contrib\admin\templates\home.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\User\Python27\lib\site-packages\django\contrib\auth\templates\home.html (Source does not exist)
django.template.loaders.app_directories.Loader: D:\myprojects\my-website\src\website\templates\home.html (Source does not exist)
Update your TEMPLATES setting, and put the app_directories loader before the filesystem loader.
If you currently have 'APP_DIRS': True, you will have to remove this, and add the loaders option.
For example, you could change your TEMPLATES setting to:
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'OPTIONS': {
'loaders': [
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
],
},
}]
My Django structure is:
testing
contacts
__init__.py
templates
contacts
index.html
(additional modules)
testing
__init__.py
templates
testing
test.html
urls.py
(additional modules)
Inside of the main URL module, testing.urls, I have a urlconf that is as follows:
url(r'^testing/$', TemplateView.as_view(template_name='testing/test.html'))
The problem is, it keeps looking in contacts.templates.contacts for the test.html file. With the existing urlcon, the debug page says the following:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/testing/test.html (File does not exist)
/Library/Python/2.7/site-packages/django/contrib/auth/templates/testing/test.html (File does not exist)
/Users/*/Developer/django/testing/contacts/templates/testing/test.html (File does not exist)
It always defaults to the..........................^^^^^^^^^^ contacts folder for some reason. Is there some other parameters for TemplateView or template_name that can control this? Any help appreciated!
Update - Inside settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
The testing/testing directory is not currently being searched by Django. The easiest fix is to add it to the DIRS setting:
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
Another option would be to add testing to your INSTALLED_APPS setting. Then Django would find your template, since you have APP_DIRS=True. However I wouldn't recommend this, because in your case testing/testing is the special directory that contains the settings.py and root url config.
By specifying "APP_DIRS": True, you are telling django to search for template files inside each app installed. Check in settings.py whether all your apps are contained within INSTALLED_APPS. If they are, you can try to force django to look for the templates in your app.
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
....
},
]
I have 2 VPS on digital ocean that are serving an app, and mydomain.com works on the first one. I need to set up a stage environment, and navigating I get:
TemplateDoesNotExist at /accounts/login/
accounts/login.html
and cannot log in. My project is visible:
Python Path:
['/home/django/bookmarks_tracker/bookmarks_tracker',
'/home/django/bookmarks_tracker',
...
but the default filesystem loader for templates is broken:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/usr/lib/python2.7/dist-packages/django/contrib/admin/templates/accounts/login.html (File does not exist)
/usr/lib/python2.7/dist-packages/django/contrib/auth/templates/accounts/login.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/bootstrapform/templates/accounts/login.html (File does not exist)
/home/django/bookmarks_tracker/helpdesk/templates/accounts/login.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/crispy_forms/templates/accounts/login.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django_extensions/templates/accounts/login.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/mptt/templates/accounts/login.html (File does not exist)
I did give it the default filesystem loader however, in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates"), os.path.join(BASE_DIR, "helpdesk/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',
],
},
},
]
The structure of TEMPLATES is identical in my working VPS, and the django versions are also the same. What could cause this to look inside apps, but not the templates dir? Thank you