How to Solve Django Error Message : AttributeError:’module’ object has no attribute ‘attribute_name’

Posted on

This is another article regarding on the error message triggered. The error itself is triggered upon executing a page which is accessed from an URL of a web-based page powered by Django framework. This is happened because of the attribute named ‘home’ doesn’t exist. Below is the actual error message shown :

AttributeError: 'module' object has no attribute 'home'

Because of the error triggered above, the page is not loaded as shown in the following image :

How to Solve Django Error Message : AttributeError:’module’ object has no attribute ‘attribute_name’

 

As it can be shown in the above image, because of the error , the page cannot be loaded at all. It is unavoidable since it is considered an error which is failing the execution of the page. Certainly it cannot be accessed since the page which is being accessed is a home page. In the home page, the URL is being processed by the URL definition in the file named url.py. It is located in the folder of the main project. Below is the description of the location :

user@hostname:~/myproject/apps$ tree -L 3 .
.
├── bin
...
├── include
...
├── lib
...├── local
...
├── src
│   ├── db.sqlite3
│   ├── apps
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   ├── wsgi.py
│   │   └── wsgi.pyc
│   ├── manage.py
...
│   └── contact
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
...
└── xxx
28 directories, 90 files
user@hostname:~/myproject/apps$

Considered the project named is ‘myproject’ based on the above output. The application which is considered as a module in the above context is contact. So, based on the error generated which is presented in the previous section of this article, the module named ‘contact’ doesn’t have any attribute named ‘home’. It is obvious since the URL definition on the url file named urls.py located in the directory as shown in the tree command output generated named ‘contact’ exist. It is shown as follows :

from contact import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
]

But the attribute as shown in the url defined, the attribute ‘home’ which is expected is there from the views.py imported from the module named ‘apps’ doesn’t exist. Below is the content of the views.py located in the folder named ‘contact’ :

from django.shortcuts import render
# Create your views here.

To solve the problem, just add a new definition or attribute named ‘home’ inside the file named ‘views.py’ as shown below :

def home(request):
    context = locals()
    template = 'home.html'
    return render(request,template,context)

Try to load the home page again off course with the additional action by creating a folder named templates and also a file named ‘home.html’ inside of it.

Leave a Reply