TemplateDoesNotExist error when extending an app template - django

I am having trouble setting up a template (under app directory) to extend a base.html that is in the root/templates/jinja2 directory
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
os.path.join(BASE_DIR, 'templates/jinja2')
],
'APP_DIRS': True,
...
},
}
]
folder structure:
apps
|---app1
|---templates
|---jinja2
|---listing.html
|---templates
|---jinja2
|---base.html
listing.html
{% extends "base.html" %}
...
The error I am getting when I pull listing.html:
TemplateDoesNotExist at ...
base.html
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /webapps/pickup/env/lib/python3.7/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /webapps/pickup/env/lib/python3.7/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /webapps/pickup/src/apps/listing/templates/base.html (Source does not exist)
What is the right way to extend to a base template in the root template folder? I am using Django 3.0.5

Make your folder structure proper
apps
|---app1
|---templates
|---app1
|---listing.html
|---base.html

APP_DIRS means U can use templates folder in app what U've registered in settings.
To specify custom path U need to add NAME parameter.
Or just try to move listing.html in app1/templates/app1/

Related

Whats the correct structure for Django templates?

I have a project in Django called: "my_site", and an app called "blog", I'm trying to render the "index.html" inside the path: my_site/blog/templates/blog/index.html. But keep getting this error:
Template-loader postmortem
Django tried loadi
ng these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\Ricardo Neto\Dev\Django_Projects\my_site\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python310\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python310\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Ricardo Neto\Dev\Django_Projects\my_site\blog\templates\index.html (Source does not exist)
The view:
def index(request):
return render(request, 'index.html')
the urls.py:
urlpatterns = [
path('', views.index),
path('posts', views.show_all_posts),
path('posts/<slug:slug>', views.show_post)
]
If i move the index.html outside the blog folder, like in this example: my_site/blog/templates/index.html
the code runs and the index.html renders without problem, but i was taught that the correct structure is to create a folder inside templates with the same name of the app.
So could anyone please explain me the way i should structure my files?
rather than keeping it within the app. I prefer to keep it under the project file. my_site/templates/app_x/index.html
├───accounts
│
├───django_project
│
└───templates
├───accounts
└───registration
In settings.py file update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py.
import os
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',
],
},
},
]
If I open separate files under templates, I render them as folder_name/index.html, otherwise directly as index.html.
Django resolves templates relative to the "templates" directory located inside the app directory and chooses the first template across all apps whose name matches. For example, if you have two apps: blog and news both with templates named "index.html" in the "templates" directory, Django would not be able to choose correctly one of them. To make a distinction you can create a subfolder inside the "templates" directory named after the corresponding application: "blog/templates/blog/index.html" and "news/templates/news/index.html". And after that you can use those templates in the view functions like this: render(request, 'news/index.html') and render(request, 'blog/index.html').
You can read about this topic here, check the remark "Template namespacing": https://docs.djangoproject.com/en/4.0/intro/tutorial03/

TemplateDoesNotExist at / but filesystem loader says it does | happening in an extends tag

I am using Django 1.5.1 with Python 2.6.6.
The error that I am getting is TemplateDoesNotExist at / when using the template extends {% extends "t_base_menu.html" %}
t_base_menu.html is in project/templates/t_base_menu.html
The template that holds the extends is in the project/home/templates/home/index.html.
The loader setting are (SITE_ROOT is correct):
TEMPLATE_DIRS = (
join(SITE_ROOT, 'templates'),
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
In the error page the Template-loader postmortem says it exists, using django.template.loaders.filesystem.Loader:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/project/templates/t_base_menu.html (File exists)
I have noticed that it is working if I move the t_base_menu.html to the project/home/templates folder.
What am I missing?

Django Template inheritance (location for extends)

I'm trying to create base.html to extend it from my apps.
My structure:
ROOT(pyshop)
polls
templates
polls
index.html (contains {% extends "base.html" %})
pyshop
templates
base.html
But it can't find base.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /home/sz/Projects/pyshop/polls/templates/pyshop/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/sz/Projects/pyshop/venv/lib/python3.5/site-packages/django/contrib/admin/templates/pyshop/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/sz/Projects/pyshop/venv/lib/python3.5/site-packages/django/contrib/auth/templates/pyshop/base.html (Source does not exist)
Any suggestions?
Your pyshop/pyshop/templates directory will not be searched by the app directories loader, because it is not an app. The usual approach is to add this directory to the DIRS list.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'pyshop', 'templates'),]
...
}
]
Secondly, the traceback makes it looks as if you are doing {% extends "pyshop/base.html" %}, not {% extends "base.html" %} as in your question. If you have {% extends "base.html" %}, then you will need to move your base.html template to pyshop/pyshop/templates/pyshop/base.html.

Django Template Does Not Exist

I know this has been covered already but none of the solutions have worked for me. I have a basic django project and it cannot find the template. Here is my views.py:
now = datetime.datetime.now()
t = get_template('index.html')
html = t.render(Context({'current_date' : now}))
return HttpResponse(html)
This is my TEMPLATE_DIRS in settings.py. The project is specified under INSTALLED_APPS
TEMPLATE_DIRS = (
'/Users/Howie/Desktop/djangoCode/mysite/Movies/templates/',
)
This is the error I get every time
Template-loader postmortem:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\Howie\Desktop\djangoCode\mysite..\templates\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\admin\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\auth\templates\index.html (File does not exist)
C:\Users\Howie\Desktop\djangoCode\mysite\Movies\templates\index.html (File does not exist)
I don't understand why django cant find my index.html file. I've tried moving the templates folder into the app itself(named Movies) and I've also had it in the root project folder but both of those didn't work. Any insight would be helpful.
The app_directories.Loader is enabled by default. This means you can put your template in an app/templates directory and be done. No need for TEMPLATE_DIRS and/or TEMPLATE_LOADERS settings. Just out of the box Django:
project/app/views.py
project/app/templates/index.html
get_template('index.html')
It's even better to use subdirectories:
project/app/views.py
project/app/templates/app/index.html
get_template('app/index.html')
This to avoid a index.html from some other app to take precedence.
If you really want a template folder in the root of your project, than don't use hardcoded paths. Do something like this:
from os.path import abspath, dirname, join, normpath
from sys import path
SITE_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(SITE_ROOT, 'templates')),
)
i had the same problem only to find out it was a typo error, i wrote templates
template/file_name/
as template which is not the Django way. The "s" is necessary
templates/file_name/

Django: sorl-thumbnail and easy-thumbnail in same project

I'm working on and project that uses two separate modular Django apps. However, one app requires easy-thumbnails and the other requires sorl-thumbnails. Unfortunately, the two thumbnail libraries make use of the template tag syntax {% load thumbnail %}, so they clash and break when a template using them tries to render.
Are there any approaches to solve this type of clash? (For example, a template option does to the effect of {% load thumbnail as easy_thumbnail %}). Am I going to have to fork one of the apps and replace one of the thumbnail libraries with another? If so, which should I choose to go with?
Thank you for considering my question,
Joe
In Django 1.9, you can use the libraries option of DjangoTemplates to include a tag library under a specified name. In the example below, the thumbnail library from sorl.thumbnail is included under the name sorl_thumbnail.
Note: the templatetag itself is not changed within the template... ie. remains thumbnail
Usage:
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "foo", "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',
],
'libraries': {
'sorl_thumbnail': 'sorl.thumbnail.templatetags.thumbnail',
},
},
},
]
your_template.html
{% load sorl_thumbnail %}
{% thumbnail mymodel.image "640x480" crop="center" as im %}
<img src="{{ im.url }}" width="{{im.width}}" height="{{im.height}}"/>
{% endthumbnail %}
Sure, just write your own stub easy_thumbnail wrapper...
Create a thumbnailtags package in one of your django apps...
...making sure it's got an empty __init__.py
In thumbnailtags/easy_thumbnail.py do something like:
from django.template import Library
from easy_thumbnails.templatetags import thumbnail
register = Library()
def easy_thumbnail(parser, token):
return thumbnail(parser, token)
register.tag(easy_thumbnail)
Use {% load easy_thumbnail %}
Note:
You might also be able to do 'import thumbnail as easy_thumbnail, and skip the def easy_thumbnail bit, tho I've not tried that.
This blog link shows how to handle this.
https://timmyomahony.com/blog/using-sorl-thumbnail-and-easy-thumbnails-same-template/
(previously
http://timmyomahony.com/blog/2012/10/22/using-sorl-thumbnail-and-easy-thumbnails-same-template/)
UPDATE 2015
I had to do the following modifications to Tom Christie's answer in order to get this to work:
create a templatetags package in one of you local apps. It is important to name it templatetags. See django docs for template tags.
... make sure it has an __init__.py, empty or not.
In templatetags/easy_thumbnail.py do this:
from django.template import Library
from easy_thumbnails.templatetags import thumbnail
register = Library()
def easy_thumbnail(parser, token):
return thumbnail.thumbnail(parser, token) # the important bit
register.tag(easy_thumbnail)
Use {% load easy_thumbnail %} or - load easy_thumbnail with pyjade