Django looking for wrong path for template file - django

My django project works in localhost perfectly.
And i was trying to deploy it on Ubuntu-Apache.
I did git clone
And i got this error
TemplateDoesNotExist at /
Hum ? and i pay attention to this error message
django.template.loaders.filesystem.Loader: /templates/base_layout.html (Source does not exist)
This Error came from
{% extends 'base_layout.html'%} (/home/ubuntu/django/blog/djangonautic/articles/templates/articles/article_list.html)
I hoped Django to search templates in the parent folder ({root}/templates).
The right path of it is
/home/ubuntu/django/blog/djangonautic/templates/base_layout.html
(File is there i checked)
Why my Django dosen't looking for it in (root) templates ?
in settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates', 'front/build'],
...
I Guess it is because.
Virtualenv
I created this in Ubuntu (didn't created in local)
Apache configure
I was following https://medium.com/saarthi-ai/ec2apachedjango-838e3f6014ab this post.
something wrong in settings.py
But i don't think this is wrong cause in local it works well...
Tell me anything you need to debugging i will answer quickly
SOLVED
Add os.path.join and works...

'DIRS': [os.path.join(BASE_DIR, 'templates'),],
I don't think you should leave out the BASE_DIR setting in your templates setting?

Related

How to create templates folder inside of an app in Django?

So Basically I like to create templates folder in main project directory and than I just create HTML file inside of it.
But now I want to create this templates folder inside of an app.
How can I do it?
Thanks Good people in Advance.
You can actually create a templates folder in any of your app. For as long as you have put in the this code inside your settings.py
settings.py
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
'APP_DIRS': True,
...
},
]
You can refer to the official docs of django here Overriding templates

Django Dir Templates not set correctly

I need help to load correctly the templates. Followed the django documentation and load my templates in the templates dir.
What i need, is to change default django name to my own name. I customized my base_site.html, but still the names from my code doesn't show, therefore i think the path is broken. Can someone please help me to fix the dirs correctly?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'DIRS': [os.path.join(BASE_DIR,'templates')],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
That is how my code looks like
It seems that it required a master templates dir in the main dir where the manage.py stay. ---the book:
Create a templates directory in your project directory (the one that contains manage.py). Templates can live anywhere on your filesystem that Django can access. (Django runs as whatever user your server runs.) However, keeping your templates within the project is a good convention to follow.
Open your settings file (mysite/settings.py, remember) and add a DIRS option in the TEMPLATES setting

Why is 'DIRS': ['templates'] also works?

To be able to find the template file in django, everybody said you should amend the settings.py like this:
TEMPLATES = [{
'DIRS': [os.path.join(BASE_DIR, 'templates')]
}]
but I found that this also works:
TEMPLATES = [{
'DIRS': ['templates'],
}]
My question is what is the difference between them. Why does no one recommend 'DIRS': ['templates']?
If you pass a relative path like 'templates' to the DIRS setting, it will be interpreted as relative to the current directory, e.g. the directory you're in when running ./manage.py runserver.
If you try to run the server from a different directory, then this path will probably point to a non-existing directory and your page won't render.
If you pass an absolute path instead, then your app will work properly regardless of the working directory.
Now, How to get the absolute path? Take the absolute path of the project (BASE_DIR) and add templates at the end - this is what os.path.join does for you.

Move customized django-allauth templates to directory other than 'account'

I set up django-allauth correctly following the project's documentation. I wanted to override the look and feel of the django-allauth templates so I went to the project github and downloaded the django-allauth templates folder into my accountsapp/templates directory.
My problem is that django-allauth will only look for its templates in the templates/account folder.
I want to put my templates in the templates/allauth/account folder to help keep MY template files separate from django-allauth's template files.
When I do this, django-allauth is unable to find the templates that I customized.
Here is my project structure:
projectfolder/
accountsapp/
templates/
projectsettings/
settings.py
manage.py
What I've tried:
Setting my TEMPLATES 'DIRS' to this
'DIRS': [ os.path.join(os.path.dirname(BASE_DIR), 'accountsapp', 'templates', 'allauth'), ],
With BASE_DIR being this: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).
This works, but I'm not sure that this is the best practice/solution to my problem.
If you put your templates in projectfolder/accountsapp/templates/, then the app directories loader will find them, as long as you have 'APP_DIRS': True in your template settings.
However, if you move the templates into the projectfolder/accountsapp/templates/allauth subfolder, then the app directories loader will not find them, so you will have to add the directory to DIRS as you have already done.

What is the best location to put templates in django project?

What is the best location to put templates in django project?
Placed in <PROJECT>/<APP>/templates/<APP>/template.html for app-specific templates to help with making the app reusable elsewhere.
For general "global" templates I put them in <PROJECT>/templates/template.html
From the Django book, chapter 4:
If you can’t think of an obvious place
to put your templates, we recommend
creating a templates directory within
your Django project (i.e., within the
mysite directory you created in
Chapter 2, if you’ve been following
along with our examples).
This is exactly what I do, and has worked great for me.
My directory structure looks something like this:
/media for all my CSS/JS/images etc
/templates for my templates
/projectname for the main project code (i.e. the Python code)
Following up from Dominic and dlrust,
We use a setuptools source distribution (sdist) to package our django project and apps to deploy in our different environments.
We have found that the templates and static files need to be under the django application directories so that they can be packaged up by setuptools.
For example, our template and static paths look like:
PROJECT/APP/templates/APP/template.html
PROJECT/APP/static/APP/my.js
For this to work, the MANIFEST.in needs to be modified (see http://docs.python.org/distutils/sourcedist.html#the-manifest-in-template)
An example of the MANIFEST.in:
include setup.py
recursive-include PROJECT *.txt *.html *.js
recursive-include PROJECT *.css *.js *.png *.gif *.bmp *.ico *.jpg *.jpeg
Also, you need to confirm in your django settings file that the app_directories loader is in your TEMPLATE_LOADERS. I think it's there by default in django 1.4.
An example of the django settings template loaders:
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
Just in case you are wondering why we use sdists instead of just coping rsync files; it's part of our configuration management workflow where we have a single build tarball that is deployed with PIP unchanged into test, acceptance and production environments.
DJANGO 1.11
add templates folder where the manage.py exist,which is your base directory.
change the DIRS for TEMPLATES as following in your settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
Now to use the template by using the code ,
def home(request):
return render(request,"index.html",{})
in views.py.
this works completely fine for django 1.11
Django 1.10
TEMPLATE_DIRS is deprecated.
Now we need to use TEMPLATE, introducing in Django 1.8 like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},
]
Once you have defined TEMPLATES, you can safely remove ALLOWED_INCLUDE_ROOTS, TEMPLATE_CONTEXT_PROCESSORS, TEMPLATE_DEBUG, TEMPLATE_DIRS, TEMPLATE_LOADERS, and TEMPLATE_STRING_IF_INVALID.
About the best location, Django looking for template like this :
DIRS defines a list of directories where the engine should look for template source files, in search order.
APP_DIRS tells whether the engine should look for templates inside installed applications. Each backend defines a conventional name for the subdirectory inside applications where its templates should be stored.
More information :
https://docs.djangoproject.com/en/1.10/topics/templates/#configuration
This is more a personal choice at the project-level. If you are talking about apps that need to be pluggable, then a templates directory in your app is the place where they go default. But project-wide, it is what works best for you.
I understood TEMPLATE_DIRS requires an absolute path. And I don't like absolute paths in my code.
So this is working well for me, in settings.py:
import os
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(os.path.realpath(__file__)),
"../APPNAME/templates")
)
Previous solution didn't work in my case. I used:
TEMPLATE_DIRS = [ os.path.join(os.path.dirname(os.path.realpath(__file__)),"../myapp/templates") ]
You could also consider having your templates in a database, using django-dbtemplates. It is also setup for caching, and the django-reversion application which helps you keep old versions of your templates around.
It works quite well, but I'd prefer a little more flexibility on the import/sync to/from filesystem side.
[edit: 20 Aug 2018 - this repository is no available, one with the same name is available at https://github.com/jazzband/django-dbtemplates and was updated 8 months ago. I no longer use Django in any meaningful way, so can't vouch for this.]