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'))]
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
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',
],
},
},
]
I set an address for the DIRS in TEMPLATES setting in my project's settings.py file and also i change APP_DIRS to False.
now, all of the templates will running but when i go to localhost:8000/admin, it generates an error that said "Template Does Note Exists".
TemplateDoesNotExist at /admin/
admin/index.html
i dont want to APP_DIRS be True.
how can i solve this problem?
this is the settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': False,
'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 you dont want APPS_DIR to be TRUE, copy templates of admin app to your template directory. When you set APP_DIR to True django will check for a directory named templates in each apps and if found load specified templates from that directory. Else django will check for a directory with same name as of app in the global template directory if found render specified template.
In you case APP_DIR is set to False and there is no directory named as admin in global template folder. So copy template directory from django/contrib/admin/ of your virtual environment and place it in your global template directory. I suppose your global template directory is in the project directory itself.
os.path.join(BASE_DIR, 'templates'),
HTH :)
I am facing strange very issue today. I am getting TemplateDoesNotExist (see first image) but when I tried to debug the template source with debug-toolbar it is correctly showing the templates path (see image 2) More strangely, when I clicked on specific templates button it is correctly showing the source of template.
This is the first time I am facing such issues. Can someone please explain why I am getting this error.
EDIT: Adding settings.py file(relavant portion)
SETTINGS_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(SETTINGS_PATH, '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',
],
},
},
]
Thanks
)
I had the similar issue with Django 1.9 . I just changed the DIRS in TEMPLATES of settings.py file.
Try this
'DIRS': [os.path.join(BASE_DIR,'templates')],
Instead of your
'DIRS':[os.path.join(SETTINGS_PATH,'templates')],
Try this code instead of your TEMPLATES
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',
],
},
},]
I upgraded to 1.9 today and suddenly had the same problem. For me it seems that adding " 'APP_DIRS': True, " to the templates does the trick (I toggled a few times by adding/deleting this and it works/fails).
So what does APP_DIRS do: if I understand the documentation ( https://docs.djangoproject.com/en/1.9/ref/templates/api/ ) correctly it reads the default Django templates if True. Basically, for 95% of all projects this should be the case.
I'm going through the very basic tutorial here and I am using my own base_site.html template instead of the default one, which has a different header text.
I copied the default template into my template folder, modified it, then linked the new template in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(__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',
],
},
},
]
No dice.
When I went a much simpler route and instead simply linked it with the single line below, it worked easily:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates/')]
Which is great! But now I want to know why the original code doesn't work.
If it helps, my folder structure is:
- Scripts
- mysite
- mysite
- polls
- templates
- admin
base_site.html
The TEMPLATES setting will be introduced in the django 1.8. It is unavailable in the current django 1.7.
So read the tutorial for the actual version of django you using.