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

Posted on

Introduction

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 manage.py 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 http://127.0.0.1:8000/
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 ‘urls.py’. 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/urls.py", 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 ‘urls.py’ exist in the application folder :

path('',include(app.urls))

Below is the full and the complete content of the ‘urls.py’ 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 urls.py script. Turns out, the main culprit is in the urls.py in the project folder. The actual mistakes is in the way for including the ‘urls.py’ file of the application inside the ‘urls.py’ of the project. The following snippet code from the ‘urls.py’ project folder is the wrong one :

path('',include(app.urls))

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 ‘urls.py’ of the project folder into the following line :

path('',include('app.urls'))

The above script is just trying to include the ‘urls.py’ of the application into the ‘urls.py’ 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 ‘urls.py’ 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
    ...
    ├── manage.py
    ...
    ├── users
    ...
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── users_list
        ├── admin.py
        ├── apps.py
        ├── forms.py
        ├── __init__.py
        ├── migrations
        │   ├── 0001_initial.py
        │   ├── __init__.py
        │   └── __pycache__
        │       ├── 0001_initial.cpython-36.pyc
        │       └── __init__.cpython-36.pyc
        ├── models.py
       ...
        ├── templates
        │   ├── base.html
        │   ├── edit.html
        │   └── home.html
        ├── tests.py
        ├── urls.py
        └── views.py

23 directories, 78 files
user@hostname:~/python/my-django$ 

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

Leave a Reply