Why console is showing templates not defined error in django

Question:

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'),
]
Asked By: Lokesh thakur

||

Answers:

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.

Answered By: shafik

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

Answered By: Iakovos Belonias

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.

Answered By: prime_hit

templates should be string. so [‘templates’] instead of [templates]

Answered By: Ayo Oseni

Replace this
‘DIRS’: [templates],
with this
‘DIRS’: ["templates"],

Answered By: alexdesalta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.