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