felix001
felix001

Reputation: 16691

Django - Unable to Locate Template from within App

Ive following the steps for adding a template, but when i try and access my URL im getting:

TemplateDoesNotExist at /

My code is below. Also I have my app in the INSTALLED_APPS in settings. And the template is located within the templates folder within my app.

View

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

def home(request):
    return render(request, 'home.html', {})

Setting

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

Layout:

edit2

Any ideas?

Upvotes: 0

Views: 43

Answers (4)

chaitanya
chaitanya

Reputation: 188

You need to add your template directory in TEMPLATES :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join((BASE_DIR),'lookup','template')],
        '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: 0

developer6811
developer6811

Reputation: 198

You need to put your .html file within, templates/<app>/
Directory structure must be like,

+ lookup/
 ...
 + templates/
  + lookup/
   - home.html

UPDATE

try,

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

def home(request):
    return render(request, 'lookup/home.html', {})

This is what followed by Django.

Upvotes: 1

felix001
felix001

Reputation: 16691

Looks like I had installed the app in the wrong place. I started from scratch and everything is working as expected.

Upvotes: 0

Sirwill98
Sirwill98

Reputation: 309

can you add this to the top of your settings if you havent:

import os
from pathlib import path
BASE_DIR = Path(__file__).resolve().parent.parent

it seems like the dirs argument is empty, could you add:

os.path.join(BASE_DIR), 'NAME_OF_TEMPLATE_FOLDER'],

Upvotes: 1

Related Questions