Bishnu Bhattarai
Bishnu Bhattarai

Reputation: 2910

Template Does not exist

I am new to Django. I use pydev eclipse as an IDE. First I created a project then an application welcome on that project. I made a folder named Templates within the project and make a file "home.html" and home.html contains

<div>
This is my first site
</div> 

I modify the settings.py file as

TEMPLATE_DIRS = ("Templates")

INSTALLED_APPS = (
    ..........#all default items
    'welcome', #the added one
)

views.py includes

from django.shortcuts import render_to_response
def home(request):
    return render_to_response('home.html')

urls.py contains

from django.conf.urls import patterns, include, url
from welcome.views import home
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'MajorProject.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^home/$', home),

)

then I run it as django project and open my browser and see on localhost:8000/home it shows error

TemplateDoesNotExist at /home/
home.html
Request Method: GET
Request URL:    http://localhost:8000/home/
Django Version: 1.6
Exception Type: TemplateDoesNotExist
Exception Value:    
home.html
Exception Location: C:\Python27\django\template\loader.py in find_template, line 131
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2
Python Path:    
['D:\\Bishnu\\BE\\4th year\\8th semester\\Major Project II\\Working\\Workspace\\MajorProject',
 'C:\\Python27\\lib\\site-packages\\distribute-0.6.35-py2.7.egg',
 'D:\\Bishnu\\BE\\4th year\\8th semester\\Major Project II\\Working\\Workspace\\MajorProject',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode',
 'C:\\Windows\\SYSTEM32\\python27.zip']
Server time:    Sun, 2 Jun 2013 14:25:52 +0545

Upvotes: 8

Views: 20538

Answers (6)

khalafnasirs
khalafnasirs

Reputation: 1

BASE_DIR = Path(file).resolve().parent.parent

This is default directory code by Django And looks like this C:\user\pc_name\django_project

But if you delete last .parent it will looks like this C:\user\pc_name\django_project\django_project

So new BASE_DIR code which is this BASE_DIR = Path(file).resolve().parent

Add this variable to TEMPLATE_DIR TEMPLATE_DIR = os.path.join(BASE_DIR, 'template")

And last code define this C:\user\pc_name\django_project\django_project\template

In the end safely uptade DIRS 'DIRS': 'TEMPLATE_DIR'

Hope you did

Upvotes: 0

Yash Marmat
Yash Marmat

Reputation: 1197

check below steps to fix

step 1:

Inside templates, 'DIRS' : [ ], metion this:

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

step 2:

Go in your views.py file and look for template_name (check its spelling and also check did you mentioned the right html file here or not)

step 3:

Check in your urls.py file wether you mentioned the right template name or not in the path

format: urlpatterns = [ path(" ", class name or function name, name = template name)

Upvotes: 0

Amitkumar Karnik
Amitkumar Karnik

Reputation: 931

in Django 1.9

in settings.py
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [BASE_DIR+r'\templates'],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            ...
        ],
    },
},
]

Upvotes: 2

Tim Reilly
Tim Reilly

Reputation: 143

If you're using Django 1.8+

You'll get this warning:

(1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS, TEMPLATE_DEBUG.

Add your template directory to the Base TEMPLATES setting under the DIRS dictionary

Like so:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            root("templates"), #### Here ####
        ],
        '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',
            ],
        },
    },
]

Upvotes: 3

nKandel
nKandel

Reputation: 2575

Try to set Templates Directory on setting.py.
as

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

Upvotes: 6

Tom&#225;š Diviš
Tom&#225;š Diviš

Reputation: 1066

Directory with templates should be named templates, not Templates (even though on windows it may be the same). Also make sure, you have application in PYTHONPATH or the correct directory structure of your project and application like:

project/
    project/
        settings.py
        ...
    welcome/
        templates/
            home.html
        views.py
        ...
  manage.py

Then you don't need to change TEMPLATE_DIRS because app_directories.Loader (enabled by default) will find the templates in your application.

Also of if you still want to change TEMPLATE_DIRS, use absolute paths, but preferred way is the app_directories.Loader.

Upvotes: 1

Related Questions