Templatedoesnotexist at django как исправить

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

Mat's user avatar

Mat

201k40 gold badges391 silver badges404 bronze badges

asked Apr 16, 2011 at 11:03

Crazy Chuck's user avatar

0

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.

answered Mar 12, 2013 at 17:30

pkout's user avatar

pkoutpkout

6,3302 gold badges44 silver badges55 bronze badges

0

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/

seenukarthi's user avatar

seenukarthi

8,20310 gold badges45 silver badges68 bronze badges

answered Apr 3, 2016 at 9:50

Bessie Tobe Bai's user avatar

1

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 /

answered Jul 16, 2018 at 22:08

himanshuxd's user avatar

himanshuxdhimanshuxd

3895 silver badges16 bronze badges

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

That worked for me.

asdf_enel_hak's user avatar

answered Oct 7, 2012 at 18:58

Jose Antonio S.A.'s user avatar

1

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"),'
)

answered May 14, 2014 at 7:59

Jobincs's user avatar

JobincsJobincs

3,8032 gold badges14 silver badges11 bronze badges

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

answered Oct 9, 2021 at 15:48

MD SHAYON's user avatar

Community's user avatar

answered Jan 26, 2012 at 12:47

andyshi's user avatar

andyshiandyshi

1591 silver badge2 bronze badges

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.

answered Dec 17, 2012 at 12:24

indi's user avatar

indiindi

3863 silver badges15 bronze badges

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

answered Feb 14, 2019 at 7:33

MagicLAMP's user avatar

MagicLAMPMagicLAMP

99510 silver badges26 bronze badges

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.

answered Aug 17, 2019 at 16:03

Shehroz Ayaz's user avatar

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

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

answered Aug 9, 2020 at 17:35

franklin forson's user avatar

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

answered Sep 5, 2020 at 11:20

Derrick Zziwa's user avatar

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

]

answered Sep 22, 2020 at 9:47

Arghya Bhattacharya's user avatar

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)

answered Feb 8, 2022 at 11:24

Lena Aretz's user avatar

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.

answered Apr 11, 2022 at 0:23

Elias Prado's user avatar

Elias PradoElias Prado

1,4692 gold badges16 silver badges32 bronze badges

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.

answered Jun 20, 2022 at 19:10

Phin's user avatar

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.

answered Jul 6, 2022 at 9:09

cosh Kabz's user avatar

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 !!!

answered Oct 15, 2022 at 21:30

Mario Shaya's user avatar

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

answered Oct 19, 2022 at 14:30

Tarun Kantiwal's user avatar

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"],
       

answered Feb 13 at 16:07

Rohit Singh's user avatar

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’)

answered Mar 5 at 16:20

Rajarshi Mandal's user avatar

TemplateDoesNotExist at /app_name/

If you are facing above template error, there is a high possibility that at-least one of the below listed point applies to you.

1. Wrong template name:

Make sure when rendering the template, you are using right name of template. 

return render(request, 'app_name/tools.html', data)

Check if template name is actually tools or it is tool?

2. Wrong app name:

In the above render statement, confirm if the appname used is correct or not.

return render(request, 'app_name/tools.html', data)

3. Installed Apps:

Please confirm if your app is listed in installed apps in settings.py file.

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your-app-here',
]

4. Template Directory Structure:

Make sure your templates are places inside a directory with same name as your app’s name which in itself is inside templates directory. Confused? 

So your templates directory structure should be as below.

app_name
|
|-templates
|    |-app_name
|    |    |-tools.html

5. Where is Django looking for templates:

Confirm if Django is looking for templates in app directories. For this you need to see templates setting in settings.py file. Check if APP_DIR is set to True. If this is set to False, Django won’t look for templates in app directories.

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

6. Project level templates:

If you are using something like below in your project’s urls.py file

urlpatterns += [
path(r'robots.txt/', TemplateView.as_view(template_name="project_name/robots.txt", content_type='text/plain')),
]

It could be any file instead of robots.txt, then make sure there is templates directory in root directory of project. This template directory is outside of every app. Directory structure would be as below.

|-project_name
|    |-templates
|    |    |-project_name
|    |    |    |-your-template-here

These template files are project level template files.

Also add 'DIRS': [os.path.join(BASE_DIR, 'templates')],  in TEMPLATES tuple in settings.py file. BASE_DIR is defined in settings.py file at the top. If not define as below.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

If you are still unable to resolve the error, feel free to comment or contact us or connect on facebook page.

Host your Django project for free.

Using IF ELSE condition in Django template

how to use IF ELSE in Django template, Syntax of IF, ELIF and ELSE in Django, Using filters within IF condition in Django template, Multiple elif (else if) confitions in Django template…

How to set a variable in Django template

Declaring a new variable in Django template, Set the value of a variable in Django template, using custom template tag in Django, Defining variables in Django template tag…

How to display flash messages in Django templates

flash messages in Django template, one-time notifications in Django template, messages framework Django, displaying success message in Django, error message display in Django…

5 lesser used Django template tags

rarely used Django template tags, lesser-known Django template tags, 5 awesome Django template tags, Fun with Django template tags,…

This error means Django was unable to find a particular template in your Django app , you might be asking yourself which template exactly is not being found ? Now for us to Know the exact template, we will have to go to our settings.py file and find this line


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

If you look at the documentation it describes the TEMPLATES variable as a list containing the settings for all template engines to be used with Django. Each item of the list is a dictionary containing the options for an individual engine.

By default Django is able to load templates from the templates subdirectory inside each installed application , the templates that cannot be found automatically are the ones which are not in our installed application , in short the templates that are in the same level as manage.py file are the ones that cannot be found automatically , read more on this here

So how do we find those templates outside of installed applications, we have to set the DIRS to point to the correct path where our template which are outside our installed appllication will be found . We will do this by setting the TEMPLATES variable like so


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

As you can see, 'DIRS' above is set to point to the path where these templates will be found by using this line os.path.join(BASE_DIR, 'templates') . Now our templates which are outside the installed apps will be found easy by Django.

Resources

  • Django templates
  • source of photo used above

Django Forum

Loading

Добавить комментарий