Wewow
Wewow

Reputation: 57

Django: TemplateDoesNotExist at / home.html

I'm trying to create first site using Django and Udemy tutorial (here) and I stuck on lesson 7: Home view. After runserver i get error:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.8.2
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/root/Documents/tryDjango/lib/python3.4/site-packages/django/contrib/    admin/templates/home.html (File does not exist)
/root/Documents/tryDjango/lib/python3.4/site-packages/django/contrib/auth/templates/home.html (File does not exist)



Traceback:
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/core/ handlers/base.py" in get_response
  132.                     response = wrapped_callback(request,     *callback_args, **callback_kwargs)
File "/root/Documents/tryDjango/src/profiles/views.py" in home
  7.     return render(request, template, context)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
  98.             template = get_template(template_name, using=using)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py" in get_template
  46.     raise TemplateDoesNotExist(template_name)

   Exception Type: TemplateDoesNotExist at /
   Exception Value: home.html

I created app profiles and my structure tree looks like this:

/tryDjango
    /src
        /tryDjango
            urls.py
            setting.py
            _init_.py
            wsgi.py
        /profiles
            admin.py
            models.py
            _init_.py
            tests.py
            views.py  
    /static
        /templates
            home.html 

settings.py

import os

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



SECRET_KEY = XXXX

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'profiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'tryDjango.urls'

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

WSGI_APPLICATION = 'tryDjango.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'

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

urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'profiles.views.home', name='home'),
]

views.py

from django.shortcuts import render

# Create your views here.
def home(request):
    context = locals()
    template = 'home.html'
    return render(request, template, context)`

I also tried commands from other and here is the output:

>>>from django.conf import settings
>>>print (settings.TEMPLATE_DIRS)
('/root/Documents/tryDjango/static/templates',)

>>> from django.template import loader
>>> print(loader.get_template('home.html'))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/    template/loader.py", line 46, in get_template
  raise TemplateDoesNotExist(template_name)
  django.template.base.TemplateDoesNotExist: home.html

Thanks a lot! P.S I use Debian.

Upvotes: 3

Views: 22673

Answers (2)

Shridhar Kulkarni
Shridhar Kulkarni

Reputation: 31

1) /templates
      <your app name>folder   
         home.html          

//this is must, create folder and give the name of app.
  in my case,
/studydjango
  /base
     /templates
         /base
            home.html
  /studydjango
  /templates

2) def home(request):
       return render(request, 'base/home.html, context)  // path must given\

3) Include 'your app name' in INSTALLED_APPS

Upvotes: 0

meshy
meshy

Reputation: 9076

It's not a good idea to put your templates in your static because files there will be readily available to the internet, and you probably don't want to be exposing your raw back end to the world ;)

Try this file structure (note that I have moved the templates directory):

/tryDjango
    /src
        /tryDjango
            urls.py
            setting.py
            _init_.py
            wsgi.py
            /templates
                home.html 
        /profiles
            admin.py
            models.py
            _init_.py
            tests.py
            views.py

Django's main template loader searches through the directories of each of the INSTALLED_APPS, and uses the first template in a templates folder that matches.

If you rely upon that (which I recommend), you will need to add 'tryDjango', to your INSTALLED_APPS.

Alternatively, the other default template loader will look in paths you have defined for templates folders.

For that, you will need to add the full path to TEMPLATES['dirs'] in Django 1.8+ or TEMPLATE_DIRS in older versions.

Upvotes: 7

Related Questions