django.template.loaders.filesystem.Loader not working for Django app - python

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

Related

templateDoesNotExist at / in django (what settings to do)

I'm new to django.
The error is coming
templateDoesNotExist at /
There is one app in my project,I've put the template folder in the app
Structure is like:
Project_name , manage.py,app_name,SQLite
The structure of the app (app_name):
other files...
Inside template folder .. (Templates(folder) -> app_name(folder) ->HTML file)
What setting I've to do in settings.py of prj to debug this error.
Did I've to include path of every template of each app. ?
Like inside setting.py in Templates
DIRS: ['BASE_DIR', "path"]
======== check & verify this ========
step-1 : check your app is registered or not
step-2: if your [templates] folder is in the app then go with the below setting otherwise put the path of your template in DIRS:['your template directory']
=== this is the default setting for a template if your templates folder in your application folder ===
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',
],
},
},
]
step-3: check in views.py template name is correct or not

Django: issue with template path for tenant

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'
],
},
},
]

Django project template loader setup

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',
],
},
}]

Django TemplateView searching in wrong place

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')],
....
},
]

Django 1.8 TEMPLATE_DIRS being ignored

This is driving me crazy. I've done something weird and it appears that my TEMPLATE_DIRS entries are being ignored. I have only one settings.py file, located in the project directory, and it contains:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'web_app/views/'),
)
I'm putting project-level templates in the /templates folder, and then have folders for different view categories in my app folder (e.g. authentication views, account views, etc.).
For example, my main index page view is in web_app/views/main/views_main.py and looks like
from web_app.views.view_classes import AuthenticatedView, AppView
class Index(AppView):
template_name = "main/templates/index.html"
where an AppView is just an extension of TemplateView. Here's my problem: when I try to visit the page, I get a TemplateDoesNotExist exception and the part that's really confusing me is the Template-Loader Postmortem:
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:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)
Why in the world are the 'templates' and 'web_app/views' directories not being searched? I've checked Settings via the debugger and a breakpoint in views_main.py and it looks like they're in there. Has anyone had a similar problem? Thanks.
What version of Django are you using? TEMPLATE_DIRSis deprecated since 1.8
Deprecated since version 1.8:
Set the DIRS option of a DjangoTemplates backend instead.
https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs
So try this instead:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Here's a link to an upgrade guide: https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/

Categories