Ai16
Ai16

Reputation: 23

TemplateDoesNotExist in get_template in loader.py - Django

Hello, this is the error I'm getting on VS code. I'm using Windows 10. I'm following this course and did exactly as they said. It's still showing me this exception. These are my folders and files.

enter image description here

This is from the settings.py file.

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates'],
    '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',
        ],
    },
},

]

This is my loader.py file from the template folder.

from . import engines
from .exceptions import TemplateDoesNotExist

def get_template(template_name, using=None):
    
    chain = []
    engines = _engine_list(using)
    for engine in engines:
        try:
            return engine.get_template(template_name)
        except TemplateDoesNotExist as e:
            chain.append(e)

    raise TemplateDoesNotExist(template_name, chain=chain)

def select_template(template_name_list, using=None):
    
    if isinstance(template_name_list, str):
        raise TypeError(
            'select_template() takes an iterable of template names but got a '
            'string: %r. Use get_template() if you want to load a single '
            'template by name.' % template_name_list
        )

    chain = []
    engines = _engine_list(using)
    for template_name in template_name_list:
        for engine in engines:
            try:
                return engine.get_template(template_name)
            except TemplateDoesNotExist as e:
                chain.append(e)

    if template_name_list:
        raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
    else:
        raise TemplateDoesNotExist("No template names provided")


def render_to_string(template_name, context=None, request=None, using=None):
    
    if isinstance(template_name, (list, tuple)):
        template = select_template(template_name, using=using)
    else:
        template = get_template(template_name, using=using)
    return template.render(context, request)

def _engine_list(using=None):
    return engines.all() if using is None else [engines[using]] 

My views.py file

from django.http import HttpResponse
from django.shortcuts import render
def home(request):
    return render(request, 'home.html')

Thanks in advance for your help.

Upvotes: 0

Views: 770

Answers (1)

Stefan Vukovic
Stefan Vukovic

Reputation: 75

You need to add app name inside templates folder. For example templates>app_name>home.html

Upvotes: 2

Related Questions