NameError: name 'index' is not defined

I have started my first django project and having an issue with adding a view to my urlpatters.

project/first_app/views.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request): return HttpResponse('test')

project/project/urls.py

from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [ path('admin/', admin.site.urls), path('', views.index, name=index),
]

This is throwing the below error in my server terminal

 File "/xxx/xxx/xxx/xxx/xxx/project/project/urls.py", line 22, in <module>
path('first_app', views.index, name=index),
NameError: name 'index' is not defined

I've played around with the path: path('first_app' quite a lot and can't seem to get it working.

2

1 Answer

It is not about which url you are calling. Somewhere your code has --Index-- which isn't defined properly, which obviously a python variable without definition. Give the --index-- as a string to name parameter in your path like this.

change your url from:

path('', views.index, name=index),

to:

 path('', views.index, name='index'),

for more info: see this example django path

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like