Manas Chaturvedi
Manas Chaturvedi

Reputation: 5540

TemplateDoesNotExist: Wrong template path

I working on a basic web application on Ubuntu. My project directory structure looks something like this:

tango
   -rango
      -migrations
   -tango
   -templates
      -rango
         -index.html

/tango/urls.py

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

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

    url(r'^admin/', include(admin.site.urls)),
    url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)

rango/urls.py

from django.conf.urls import patterns, url
from rango import views

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'))

settings.py

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

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
TEMPLATE_PATH,

)

views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    context_dict = {'boldmessage': "I am bold font from the context"}

    return render(request, 'rango/index.html', context_dict)

index.html

<!DOCTYPE html>
<html>

    <head>
        <title>Rango</title>
    </head>

    <body>
        <h1>Rango says...</h1>
        hello world! <strong>{{ boldmessage }}</strong><br />
        <a href="/rango/about/">About</a><br />
    </body>

</html>

However, when I try to access http://localhost:8000/rango/, this is the error that I'm encountering:

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/manas/D_Drive/Django/tango/rango/views.py" in index
  14.     return render(request, 'rango/index.html', context_dict)
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
  98.             template = get_template(template_name, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in get_template
  46.     raise TemplateDoesNotExist(template_name)

Exception Type: TemplateDoesNotExist at /rango/
Exception Value: rango/index.html

index.html is stored in the following path: /home/manas/D_Drive/Django/tango/templates/rango/index.html

I'm using Django 1.8 and Python 2.7 and running the application on Ubunty 14.04.

What seems to be wrong here?

EDIT:

Template settings:

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

Upvotes: 1

Views: 1781

Answers (2)

Abdullah
Abdullah

Reputation: 8995

Make sure to add your app to settings.py

INSTALLED_APPS = [
    'myapp',
    ...,
    ...,
]

Upvotes: 0

litwisha
litwisha

Reputation: 412

You have 'DIRS' an empty list in your TEMPLATES configuration, so it can't find your template folders. Just replace it with: 'DIRS': [os.path.join(BASE_DIR, 'templates')] or 'DIRS': [TEMPLATE_PATH] and it's gonna work,

Upvotes: 3

Related Questions