Philip Lumayo
Philip Lumayo

Reputation: 9

AttributeError: module 'PrivateSchools.views' has no attribute 'HomePage'

I am getting the error when try to run the server.

 File"C:\Users\admin\Desktop\InstitutionFinderWebsite\
 InstitutionFin 
 derWebsite\urls.py", line 26, in <module>path('HomePage/', 
 views.HomePage),
 AttributeError: module 'PrivateSchools.views' has no attribute 
'HomePage'

I had imported all the views from the three apps as below

from django.conf.urls import include
from django.contrib import admin
from django.urls.conf import path
from HomePage import views
from PublicSchools import views
from PrivateSchools import views

On the urls.py have tried the 2 methods below but the are not all working.

Method one here i used views. to map the urls.

urlpatterns = [
path('admin/', admin.site.urls),
path('HomePage/', views.HomePage),
path('PublicSchools/', views.PublicSchools),
path('PrivateSchools/', views.PrivateSchools),
]

This is method two in trying to solve it by trying to give the names.

urlpatterns = [
path('admin/', admin.site.urls),
path('HomePage/', views.HomePage, name='PrivateSchools'),
path('PublicSchools/', views.PublicSchools, 
name='PublicSchools'),
path('PrivateSchools/', views.PrivateSchools, 
name='PrivateSchools'),
]

Upvotes: 1

Views: 36

Answers (1)

David Louda
David Louda

Reputation: 577

The error occurs due to numerous views import not having a unique name and therefore only the last one counts.

when you do

from HomePage import views
from PublicSchools import views
from PrivateSchools import views

and then

path('HomePage/', views.HomePage),

you are effectively doing this

path('HomePage/', PrivateSchools.views.HomePage),

because PrivateSchools was imported as the last one.

Solve your problem by naming your views differently as for example

from HomePage import views as home_page_views
from PublicSchools import views as public_schools_views
from PrivateSchools import views as private_schools_views

and then for example

path('HomePage/', home_page_views.HomePage),
path('PublicSchools/', public_schools_views.PublicSchools),
path('PrivateSchools/', private_schools_views.PrivateSchools),

Upvotes: 1

Related Questions