How to Solve Error NameError: name ‘views’ is not defined in Django

Posted on


This is an article to discuss about how to solve an error with the error exist in one of the django framework script. It shows upon executing the virtual webserver to display and execute the django application. The following is the command execution for showing the django application via virtual webserver :

user@hostname:~/python/my-django/users$ python runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 30, 2019 - 20:47:50
Django version 2.2.3, using settings 'users.settings'
Starting development server at
Quit the server with CONTROL-C.

Finding the proper solution

The location of the above command execution is in the root folder or root directory of django project. Inside the project folder, there is another folder for containing the application script. Inside that folder, there is a script responsible for defining the route access page of the django application. The file name of that script is ‘’. Apparently, after editing the files, there is an error . The error is in the following output :

File "/home/user/python/my-django/users/users_list/", line 5, in <module> path('',views.home, name="home"), NameError: name 'views' is not defined

The cause of the error is in the following line of the script with the name ‘’ exist in the application folder :


Below is the full and the complete content of the ‘’ inside the application folder :

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

urlpatterns = [
    path('',views.home, name="home"),
    path('delete/',views.delete, name="delete"),

Actually, there is nothing wrong with the line as in the above script. Turns out, the main culprit is in the in the project folder. The actual mistakes is in the way for including the ‘’ file of the application inside the ‘’ of the project. The following snippet code from the ‘’ project folder is the wrong one :


I write it as path(”,include(app.urls)) and it should be using ”. It is because it doesn’t have ”. It is because i don’t import views first it should be from app import views. So, the correction to solve the error above is just changing the line above in the ‘’ of the project folder into the following line :


The above script is just trying to include the ‘’ of the application into the ‘’ of the project. So whenever there is an attempt to access the url of the application, django can recognizes the pattern. But since the syntax for including the ‘’ of the application is wrong, the error occurs as it shows in the beginning part of this article. The discussion for solving the error also exist in the following link. For a better understanding of the structure of the project and also the application of the django script, the following is the tree structure of the directories :

user@hostname:~/python/my-django$ tree
└── users
    ├── db.sqlite3
    ├── users
    │   ├──
    │   ├──
    │   └──
    └── users_list
        ├── migrations
        │   ├──
        │   ├──
        │   └── __pycache__
        │       ├── 0001_initial.cpython-36.pyc
        │       └── __init__.cpython-36.pyc
        ├── templates
        │   ├── base.html
        │   ├── edit.html
        │   └── home.html

23 directories, 78 files

The project folder is the ‘users’ folder and the application folder is the ‘users_list’ folder.

Leave a Reply