How to add template directory in django?

I'm getting TemplateDoesNotExist at /task/ error.

This is my folder structure for project.

This is my taskmate/urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('task/',include('todolist_app.urls')),
    path('todolist/',include('todolist_app.urls')),
]

This is my todolist_app/urls.py:

from django.urls import path
#from todolist_app import views
from . import views

urlpatterns = [
    path('', views.todolist, name='todolist'),
]

This is my todolist_app/views.py:

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

# Create your views here.
def todolist(request):
    return render(request, 'todolist.html',{})

This is my settings.py(important components)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [(os.path.join(os.path.dirname(os.path.dirname(__file__)),'todolist_app/templates').replace('\\','/'))],#'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',
            ],
        },
    },
]

I'm highly suspicious that the issue I'm getting is due to "DIRS" of template. I have tried couple of different things there, but none seem to have worked.

These are what I've tried there:

    'DIRS': [os.path.join(BASE_DIR,'templates')],
    'DIRS': [os.path.join(os.path.dirname(__file__),'templates').replace('\\','/')],#'DIRS': 
    'DIRS': [(os.path.join(os.path.dirname(os.path.dirname(__file__)),'todolist_app/templates').replace('\\','/'))],#'DIRS': 

I've also added "todolist_app" to installed apps.

You need to add app_name to your html. This might solve the issue

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

# Create your views here.
def todolist(request):
    return render(request, 'app_name/todolist.html',{})

Update:

make sure your settings.py TEMPLATE section is configured as below:

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

What fixed the issue was just add html/todolist.html in render. i.e this in views.py of todolist_app:

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

# Create your views here.
def todolist(request):
    return render(request, 'html/todolist.html',{})

This is not my solution.

Back to Top