AttributeError module object has no attribute in Django

In this Python Django tutorial, I will explain how to solve AttributeError module object has no attribute in Django.

Recently, when I was working on a Django project, I came across an issue where I have an error “module object has no attribute”. So, after looking at multiple solutions, I found a solution that helped me solve this issue.

Here we will see:

  • AttributeError: module object has no attribute in Django
  • Solution of attribute error “module object has no attribute” in Django

AttributeError module object has no attribute in Django

Recently, I was working on Django, I faced an issue Django attribute error module object has no attribute. In this issue, the specific attribute is missing from the module.

Generally, this error occurs when the view we define is not matched with the URL path. For example, we create a view with the name ‘home’ in views.py and we specify its path with the name ‘views.index’ in the app urls.py in Django.

Here, is the sample screenshot where the “module object has no attribute” error occurs in Django.

AttributeError module object has no attribute django
AttributeError in Python Django

Read: ModuleNotFoundError: No module named Django

Solution of attributeerror “module object has no attribute” in Django

To solve this Django AttributeError: module object has no attribute. We have to rectify the name of the view defined in the URL path in urls.py located inside the app directory.

Let’s see an example to clearly understand the concept.

In this example, I will create a Django Project named AttributeErrorProject with an app inside it named MyApp with a function-based view named home.

Note: The name of the project, the app, and the view depends upon your project. I take this scenario only for understanding.

Start a Django project, by opening the terminal and entering the following command. Here, AttributeErrorProject is the name of the Django Project.

django-admin startproject AttributeErrorProject

It will make a folder called AttributeErrorProject and to enter the project, type the below command in the terminal.

cd AttributeErrorProject

Create a Django app named MyApp inside this project folder, by typing the below command in the terminal.

python manage.py startapp MyApp

Add the app name to the INSTALLED_APP list located in the settings.py file, to activate this app.

AttributeError module object has no attribute in python django
Settings.py in django

By default, Django includes a urls.py file in the project directory to map the newly constructed app inside of it. To do so, add the below code in it.

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('MyApp.urls')),
]

To define the main logic for the application, open the views.py file and add the code given below.

from django.shortcuts import HttpResponse

# Create your views here.

def home(request):
    return HttpResponse('Welcome to PythonGuides!!!')

Here, we have created a function-based view name home that returns HttpResponse.

Now, we must map the view with the URL in order to call it, thus we must create a file called urls.py in the app directory. Include the code below in it.

from django.urls import path
from MyApp import views

urlpatterns = [
    path("", views.home, name="home"),   
]

Here, we have specified the path of view named home as views.home.

To launch a development server type the below-given command in the terminal.

python manage.py runserver

It will successfully, open the page and return the HttpResponse without any error.

module object has no attribute in django
Solution of attribute error “module object has no attribute”

But, if we specify the incorrect name of the view in the path in urls.py, we will get a Django Attribute Error “module object has no attribute”. For example, by mistake, I specify the name of the view as an index in the urls.py.

from django.urls import path
from MyApp import views

urlpatterns = [
    path("", views.index, name="home"),   
]

Now, start the development server and see it will produce an error AttributeError: module ‘MyApp.views’ has no attribute ‘index’ in Django as shown below.

AttributeError module object has no attribute in django
AttributeError module object has no attribute in Django

So, by specifying the correct name of the view in the urls.py file we can solve the above-defined error in Django.

Read: Python Django get enum choices

Conclusion

With this, we have understood how to solve issues related AttributeError module object has no attribute in Django. Moreover, to solve this issue in Python Django, we have covered a step-by-step procedure.

Additionally, we have also covered the following topics.

  • AttributeError module object has no attribute in Django
  • Solution of AttributeError module object has no attribute in Django

You may also like to read the following Python Django tutorials.