'Why console is showing templates not defined error in django

I am new to django , I am facing one while django running server , I have copy and paste my code please tell me what is wrong with my code

'DIRS': [templates],
NameError: name 'templates' is not defined

in settings.py file , I have put templates in [] brackets

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 views.py

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

def index(response):
    return render(request,'index.html')

this is urls.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('about', views.about, name='about'),
]


Solution 1:[1]

templates should be string. so ['templates'] instead of [templates]

Solution 2:[2]

You need to change DIRS here

'DIRS': [os.path.join(BASE_DIR, 'templates')],

Here, Django expects it has a template path but you provide templates variable which does not have any path.

Solution 3:[3]

While others have already given right answers on how to fix it, but I would like to point out a minor thing that you are getting the NameError because you haven't defined the name templates.

The NameError is raised when the variable you are using is not defined anywhere in the program (if you defined it outside the current scope you will be getting UnboundLocalError).

If you define the name templates as a string for the absolute path to your template folder, this will work. Still, never use absolute path in your Django application cause it will become a headache while deployment.

Solution 4:[4]

You have to change your line to this

'DIRS': [os.path.join(BASE_DIR, 'templates')],

In the begging of the settings files you can see the BASE_DIR

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

Changing DIRS to

'DIRS': [os.path.join(BASE_DIR, 'templates')],

Will help the app to find the templates folder. Just a personal advice in each app store the templates/appname folder for the specific app. It will help you to have a more robust Django app.

for example you have the polls app, inside the polls app it will be like this

pollsApp/templates/pollsApp/index.html etc.

Also here is the official guide, it might help you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ayo Oseni
Solution 2 shafik
Solution 3 prime_hit
Solution 4 Iakovos Belonias