Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

web application in python using Django

+1 vote
asked Apr 26, 2019 by Priyanka
I have done All the steps accordingly right but could not get output . It shows me error of name error, path is not defined.

1 Answer

0 votes
answered 10 hours ago by Shaurya (410 points)

Likely cause

A Python NameError: name 'path' is not defined' in a Django project almost always means your urls.py (or the module that builds urlpatterns) is using the path symbol but you never imported it into that file.

Quick checklist to fix it

  • Open the urls.py that the traceback points to and check the top of the file for an import like:

from django.urls import path, include

If that line is missing, add it.

  • If you used url() or re_path() instead of path, import the correct symbol:

from django.urls import re_path  # or: from django.conf.urls import url  (legacy)

  • If path is imported but still failing, ensure there is no local variable or function named path shadowing the import.

  • Confirm Django version: path() was introduced in Django 2.0. On very old Django versions you must use url() or upgrade Django.

  • Restart the dev server after changes: python manage.py runserver.

Minimal correct examples

Project urls.py (project root)

# project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),  # include your app urls
]

App urls.py (myapp/urls.py)

# myapp/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('items/<int:pk>/', views.detail, name='detail'),
]

If the error appears elsewhere

  • Traceback location matters. Read the full traceback: it tells you which file and line raised the NameError. Fix that file.

  • Accidental typo: maybe you wrote Path or pth instead of path.

  • Imported in wrong file: importing path in one module does not make it available in another.

Debugging tips

  • Run the server and reproduce the error; copy the full traceback — it shows the exact file and line.

  • Add a quick sanity print at the top of the file to confirm it’s the file you expect:

print("Loading project.urls")

  • In a Python shell inside your project (python manage.py shell) try:

from django.urls import path
print(path)
to confirm path is importable in that environment.

Common related mistakes

  • Using from django.conf.urls import url and then trying to use path without importing it.

  • Forgetting include import when using include('app.urls').

  • Copying example code from a tutorial that uses path but running an older Django release.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...