TemplateDoesNotExist at/
Asked Answered
T

23

26

here is my site url http://webtrick.heliohost.org/ my template directory settings:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__) , 'templates').replace('\\','/')
)

view.py

from django.http import HttpResponse
from django.template import Template , Context
from django.shortcuts import render_to_response
def index(request):
        return render_to_response('index.html')

url.py

from django.conf.urls.defaults import patterns, include, url
from webtrickster.views import index
urlpatterns = patterns('',
    url(r'^$', index)
)

i don't know what's wrong with this code any help appreciated

Tartarean answered 16/4, 2011 at 11:3 Comment(0)
F
7
 os.path.join(os.path.dirname(__file__) ,'../templates').replace('\\','/')

That worked for me.

Franciscka answered 7/10, 2012 at 18:58 Comment(1)
Is it just me, or should there be a single quotation mark before ../templates' ?Derris
L
61

Make sure your Django app is in the INSTALLED_APPS in settings.py. That was my problem with it. So if you have the templates folder in your polls app folder, you need to add the 'polls' at the end of the installed apps in the settings file.

Lowtension answered 12/3, 2013 at 17:30 Comment(0)
O
30

Add you application into setting.py end of the INSTALLED_APPS like below:

INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
    'mysite'                   
  ]

and your templates dir should be in mysite like

mysite
    -settings.py
    -templates/
Ozenfant answered 3/4, 2016 at 9:50 Comment(1)
It does not load css files in the templates directory.Steinmetz
M
10

I wasted a lot of time trying to figure out what was wrong with my 'templates' directory and found out that :

You may have forgotten to add TEMPLATE_DIR in the following segment of 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',
            ],
        },
    },
]

So it should have 'DIRS': [TEMPLATE_DIR,], instead of 'DIRS': [],

Also see answers for : TemplateDoesNotExist at /

Mansoor answered 16/7, 2018 at 22:8 Comment(0)
F
7
 os.path.join(os.path.dirname(__file__) ,'../templates').replace('\\','/')

That worked for me.

Franciscka answered 7/10, 2012 at 18:58 Comment(1)
Is it just me, or should there be a single quotation mark before ../templates' ?Derris
E
5

First you need to make one folder named 'templates' in your django project folder then, you can add template directory by two ways open your settings file then add any of the following code

1) You can specify your template path directly by

TEMPLATE_DIRS = ( 'D:/Django/web/Kindset/templates' #your file path ,this is my template directory path )

2) Or if you want to use generic path means use

TEMPLATE_DIRS = ( 'os.path.join(BASE_DIR, "templates"),' )

Estradiol answered 14/5, 2014 at 7:59 Comment(0)
S
1

Move your "templates" folder to the main and not in any sub folder. Or on the same level as other projects folder. This might have been the reason why Django was not locating my templates. My project worked after doing this.

Surplusage answered 17/8, 2019 at 16:3 Comment(0)
T
1

You may forgot to install you app

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tutorials'
]

Make sure you also add dirs properly in 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',
            ],
        },
    },
]
Tea answered 9/10, 2021 at 15:48 Comment(0)
G
1

If you have tried some of the solutions above and it not worked try checking whether the template is within the correct folder.

Template outside of the templates/appname folder:

appname/
  |
  |__templates/
  |  |
  |  |__appname/
  |     |__appname_form.html
  |__appname_list.html <------ outside of the appname/ folder (WRONG)

Like this if you have a url that send to this template, Django will through a TemplateDoesNotExist since the template does not follow the path you placed in TEMPLATES.DIRS.

Try fixing like:

Template inside of the templates/appname folder:

appname/
  |
  |__templates/
  |  |
  |  |__appname/
  |     |__appname_form.html
  |     |__appname_list.html <------ inside of the appname/ folder (RIGHT)

Therefore, like this you may fix this exception.

Greenes answered 11/4, 2022 at 0:23 Comment(0)
R
0

TEMPLATE_DIRS = ( "mysite/templates" )

TemplateDoesNotExist - file exists, no permissions issue

Reprehensible answered 26/1, 2012 at 12:47 Comment(0)
A
0

My templates folder wasn't a python package because it was missing the __init__.py file. This might have been the reason why Django was not locating my templates.

Adelleadelpho answered 17/12, 2012 at 12:24 Comment(0)
R
0

Many good answers here, but on updating an old project (nightmare=zinnia, django_cms, AUTH_USER_MODEL = 'accounts.CustomUser') that was not maintained for many moons from django 1.6 to django 1.7 as stage one of a very long process, I found nothing worked. Then I noticed that the errors had quotes around the template it was trying to load:

/home/vagrant/venv/crowd88/local/lib/python2.7/site-packages/djangocms_admin_style/templates/'core/includes/ga.html' (File does not exist)

So I looked at the source and found that quotes are stripped this way:

# lib/python2.7/site-packages/cms/utils/plugins.py
...
template = get_template(force_unicode(node.template).strip('"'))
...

Then I noticed that single quotes were being used in the loaders

{% include core/includes/ga.html' %}

So the fix was

{% include "core/includes/ga.html" %}

using a regex process

Resurrectionist answered 14/2, 2019 at 7:33 Comment(0)
L
0

make sure your dirs directory in settings.py looks like this

'DIRS': [os.path.join(BASE_DIR, 'templates')],

Laity answered 9/8, 2020 at 17:35 Comment(0)
P
0

By default, inside the settings.py file, the 'DIRS' array is empty as you can see below.

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

So to solve the issue, just add BASE_DIR / 'templates' to the array to look like below. And you are good to go.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
]
Polygynist answered 5/9, 2020 at 11:20 Comment(0)
C
0

Go to the settings.py file and check the TEMPLATES section. Notice the DIR value, by default it should be []. Add "os.path.join(BASE_DIR, 'templates')" inside this. It should look 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',
        ],
    },
},

]

Condyle answered 22/9, 2020 at 9:47 Comment(0)
P
0

I encountered the same error. After checking TEMPLATE_DIR and INSTALLED_APPS a thousand times I noticed a small typo in one .html template:

{% extends "my_app/base.html " %}

should have been

{% extends "my_app/base.html" %}

(notice the white space in the first command)

Pericynthion answered 8/2, 2022 at 11:24 Comment(0)
S
0

if {% extends "blog/base.html" %} make sure base.html is in the blog folder and not in Templates or any other directory. That solved it for me.

Slickenside answered 20/6, 2022 at 19:10 Comment(0)
C
0

This worked for me: 1.Make a folder in your app called static and create the css file in the static folder 2.Make another folder still in your app called templates and create the html file there

  1. On the first line of your html file write this: {% load static %}
  2. Link the css file inside the head tag as follows: href="{% static 'index.css' %}"
  3. Thank me later.
Cholula answered 6/7, 2022 at 9:9 Comment(0)
B
0

Got the same problem ! I've already followed all solutions provided above but pfff ! I were lost and maybe someone could face it too! So I solved it by only return render(request, 'profile.html') and not return redirect(request, "profile.html")

Happy debugging !!!

Beilul answered 15/10, 2022 at 21:30 Comment(0)
D
0

After debugging a lot, i found out that the path which i was giving in views.py to render the template is wrong so it was showing TemplateDoesNotExist at/ error

for Example the mistake i was making is this

Wrong code :- return render(request, 'show/index.html', {'product_objects': product_objects})

Right code :- return render(request, 'shop/index.html', {'product_objects': product_objects})

the app name was shop not show.

Hope it will help someone

Delhi answered 19/10, 2022 at 14:30 Comment(0)
I
0

I think the answer is pretty simple just give the right path Use this if you have your templates folder in your app

    BASE_DIR = Path(__file__).resolve().parent.parent
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
     
    
 'DIRS': [BASE_DIR,"templates"],
       
Indeterminacy answered 13/2, 2023 at 16:7 Comment(0)
L
0

Include the template folder inside your app. Refer this image for directory structure

Luhey answered 27/12, 2023 at 21:0 Comment(0)
W
0

Just in case someone makes the same stupid mistake. Got similar error when I forgot to enclose included template in quotes, and what is really confusing it highlights a different line in a base template in the error screen. Basically:

{% include blog_entry.html with n=i tags=i.tags %}

instead of

{% include 'blog_entry.html' with n=i tags=i.tags %}

see quotes around blog_entry.html

Wandie answered 30/12, 2023 at 9:21 Comment(0)
S
-1

This is the little requirement:

return render(request, 'templetes/test.html') with 'DIRS': [os.path.join(BASE_DIR) ,'templates'],

NOT return render(request, 'test.html')

Susurrate answered 5/3, 2023 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.