Template does not exist

I am new to Django. I am using pydev eclipse as an IDE. First I created a project, then the application welcomed this project. I created a folder called "Templates" in the project and created the file "home.html", and home.html contains

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

I am changing 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 a django project and open my browser and see on localhost: 8000 / home it shows an 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 
+7
source share
4 answers

Try setting the template directory to setting.py .
as

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

If you are using Django 1.8+

You will receive the following 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 the template directory to the base parameter TEMPLATES in the DIRS dictionary

Same:

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

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': [ ... ], }, }, ] 
+1
source

The directory with templates should be called templates , not templates (although this can be the same in windows). Also make sure you have the application in PYTHONPATH or the correct directory structure of your project and application, for example:

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

Then you do not need to change TEMPLATE_DIRS , because app_directories.Loader (enabled by default) will find the templates in your application.

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

0
source

All Articles