I'd like to store templates that all app templates will extend from.
Current layout of project
mysite/
-(virtualenv folders...)
- wesdrew/ # project root
- static/
- templates/
- base.html # simple templates to test loading
- wesdrew/ # 'home page' app
In wesdrew/settings.py:
PROJECT_TEMPLATES='../templates'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [PROJECT_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',
],
},
},
]
Is there a way to override the template engine looking for '/templates/wesdrew'
Related
Django can't find needed template from my app. I named directory "app/template/app/template.html", my settings:
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',
],
},
},
]
Also I add my app name in INSTALED_APPS.
I tried move my template in project's directory and its work! But I just wanna understand, why loader didn't check my app directory
How to specify a path to base_site.html that would have views the blog saw it.
enter image description here
You have to do 2 things
Edit the templates settings to include templates folder
Call the template by ‘admin/base_site.html’
First of all add this in your settings.py
'DIRS': [os.path.join(BASE_DIR / "templates")]
For Example:
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 change the path or just paste this, and let me know..
return render(request, "admin/base_site.html", {"tasks":tasks})
I want to create a directory of templates of Django in community edition of PyCharm. So I run this command:
django-admin startproject mysite
but it does not create directory of templates.
I create it manually but always when I run server to URL that are in templates directory. It always returns
Django templates does not exist
create templates directory in your project and complete the TEMPLATES setting in settings.py like this:
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',
],
},
},
]
How to define a directory for storing all templates of my app? I am having a hard time finding the explanation how to set templates directory in Django settings.py file.
To set your path to templates folder you need to set your DIRS key with the value in which you are giving your path to templates as shown in the example below where at first i am setting a variable TEMPLATES_DIR where BASE_DIR give me the path to the folder where my manage.py is being kept and i concatinate projects directory alongwith templates directory because in my case i kept it inside project directory.
For example :-
TEMPLATES_DIR = os.path.join(BASE_DIR, 'project','templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'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',
],
},
},
]
So, Here's the explaination What i did is set the TEMPLATES_DIR variable with reference to base directory that will take the path to folder where manage.py of your django project is being kept now i am concatinating the projects and templates directory just to navigate the exact folder structure that you kept for your templates in your project.
I hope you found this helpful. if you have any more doubts i'll be happy to answer those also.
Happy Coding. :-)
The way to set your templates directory in Django is by setting the value of the 'DIRS' key in TEMPLATES inside the settings.py like so (assuming it is present just inside your base directory (BASE_DIR):
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',
],
},
},
]
In Django 3, the templates directory can be added to 'DIRS' like this as well:
'DIRS': [str(BASE_DIR.joinpath('templates'))]
I Have been reading about templates in the django book, the author has suggested to add template path in the setting.py variable TEMPLATE_DIRS but my setting.py has no `TEMPLATE_DIRS'(I understand why is it so)and I could find the below
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), ’templates’).replace(’\\’,’/] #1
'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',
],
},
},
]
1)Does the above change I have madein the settings.py lead to me in the correct direction or is there any better way to do it? I wanna make myself sure before I put my self into others and end up in mess.
TEMPLATE_DIRS variable is deprecated since Django 1.8 (https://docs.djangoproject.com/en/1.9/ref/settings/#template-dirs).
Your setup is fine, but you can clean it up a bit, for example:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'templates', 'some_other_dir'),
# other paths
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]