martianwars
martianwars

Reputation: 6500

TemplateDoesNotExist error in Django?

I searched a lot of similar questions on StackOverflow but couldn't find an answer to this problem. I was following the guide at tango with django. My file directory looks like this :-

|-- db.sqlite3
|-- manage.py
|-- media
|   `-- rango.jpg
|-- rango
|   |-- admin.py
|   |-- admin.pyc
|   |-- apps.py
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- migrations
|   |   |-- __init__.py
|   |   `-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- tests.py
|   |-- urls.py
|   |-- urls.pyc
|   |-- views.py
|   `-- views.pyc
|-- rango.db
|-- static
|   |-- rango1.jpg
|   `-- rango.jpg
|-- tango_with_django_project
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
`-- templates
    `-- rango
        |-- about.html
        |-- index.html
        `-- order.html

On starting the server, I get a TemplateDoesNotExist error which looks like this :-

Django tried loading these templates, in this order:

Using engine :
django.template.loaders.filesystem.Loader: /home/kalpesh/tango_with_django_project/templates/templates/rango/about.html (Source does not exist)

What's strange to me is the templates/templates that appears in the path name. Here is my settings.py file :-

#New stuff
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
DATABASE_PATH = os.path.join(PROJECT_PATH, 'rango.db')

#Template new stuff
TEMPLATE_PATH = os.path.join(PROJECT_PATH, "templates")
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS' : [TEMPLATE_PATH],
        'APP_DIRS': False,
        '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',
            ],
        },
    },
]

What's going wrong? If I make identical HTML files inside ./templates/templates/rango, the app works.

Upvotes: 0

Views: 213

Answers (1)

Gianluca Mancini
Gianluca Mancini

Reputation: 1312

Django allows you to reference the templates relatively to the root of the directories listed in DIRS (in this case only templates); so you only need to pass rango/about.html.

Upvotes: 1

Related Questions