Django project template loader setup - python

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

Related

TemplateDoesNotExist when using Django package

I followed this Django tutorial: https://docs.djangoproject.com/en/1.10/intro/reusable-apps/.
I have a project called oldcity and an app called oldantwerp. The app is located in a parent directory called django-oldantwerp and the app directory itself has a subdirectory templates. The index.html file that my project is looking for is situated like so:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html
I tried to use this app with my project by including it in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'oldantwerp'
]
and in urls.py (in the projec), like so:
urlpatterns = [
url(r'^oldantwerp/', include('oldantwerp.urls')),
url(r'^admin/', admin.site.urls),
]
When I go to the admin page, everything works, but when I try to open the index page I get this error:
TemplateDoesNotExist at /oldantwerp/
It says it tried to locate the index.html file like so:
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/admin/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/auth/templates/oldantwerp/index.html (Source does not exist)
And it also tried searching for another file: place_list.html, which is strange because I don't think I have such a file.
What could be wrong?
EDIT
This is the code of views.py in the oldantwerp folder:
class IndexView(generic.ListView):
template_name = 'oldantwerp/index.html'
context_object_name = 'places'
def get_queryset(self):
return Place.objects.order_by('id')[:]
EDIT
Maybe worth mentioning: it all did work when I just had a folder oldantwerp as a subdirectory in the oldicty project folder. This error only occurred after I started implementing it from an external package.
EDIT
These are my template settings in settings.py:
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',
],
},
},
]
One most likely problem could be you have got the template name wrong in your view. Ensure you use the template oldantwerp/index.html in the view, not just index.html.
If you look at your stacktrace you will see that you application tries to load the template from: * django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist).
If I read your first statement correctly your directory structure is:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html.
So you can either create a directory structure proper for django to identify your templates directory or you can update your templates constant from the settings file and point django to a physical location where it can find your templates.
You can try to set DIRS in settings.py like this: os.path.join(BASE_DIR, 'templates'), delete the oldantwerp folder from your templates folder and add index.html in templates.
After that, edit template_name = 'oldantwerp/index.html' to template_name = 'index.html'

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.template.loaders.filesystem.Loader not working for Django app

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

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/

Djangos TEMPLATE_DIRS not found

I am trying to create a login page in my django application. I created a "templates" folder on the root directory of my application.
Then on my settings.py I wrote this code.
TEMPLATE_DIRS = (os.path.join(BASE_DIR,'templates'),)
And it is giving this feedback:
TemplateDoesNotExist at /login/
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:
/Users/julianasakae/Desktop/DjangoProject/demo/lib/python3.4/site-packages/django/contrib/admin/templates/login.html (File does not exist)
/Users/julianasakae/Desktop/DjangoProject/demo/lib/python3.4/site-packages/django/contrib/auth/templates/login.html (File does not exist)
/Users/julianasakae/Desktop/DjangoProject/boardgames/main/templates/login.html (File does not exist)
I tryed everything, it does not seems to work.
Any suggestions?
What version of Django are you using? It appears that TEMPLATE_DIRS was used prior to 1.8 but in the current version it has changed to a DIRS option in the TEMPLATES setting.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/home/html/templates/lawrence.com',
'/home/html/templates/default',
],
},
]
Template DIRS Option Docs
Well it is not a good solution, but try hardcoding the full path.

Categories