Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

AttributeError: module 'polls.views' has no attribute 'index' error in Django

103 views
Skip to first unread message

Олександр Рябов

unread,
Dec 5, 2024, 7:02:36 AM12/5/24
to Django users
Hello! I'm facing an issue when running my Django project. I get the following error:

AttributeError: module 'polls.views' has no attribute 'index'

I've checked the following:  
    1) In "polls/views.py", I have defined the index function:
        from django.http import HttpResponse
        
        def index(request):
            return HttpResponse("Hello, World!")

    2) In polls/urls.py, the path to this function is correctly defined:
        from django.urls import path, include
        from . import views

        urlpatterns = [
            path("", views.index, name="index"),
        ]
    3) In the main mysite/urls.py, the URLs from polls are included:
        from django.contrib import admin
        from django.urls import include, path

        urlpatterns = [
            path("polls/", include("polls.urls")),
            path("admin/", admin.site.urls),
        ]

However, when I run the server with the command:
    " python manage.py runserver "  

I get the error mentioned above. I have also made sure that all imports are correct and that the project is using the right Python version.

What could be the cause of this error and how can I fix it? Thank you!
P.S.
- I am writing my code in Visual Studio Code and have attached two pictures for reference.
- The first picture shows the general structure of my project.
- The second picture displays the full error message from the command line(*try to enlarge it, I hope you can see everything.)
#1

photo_1.png
#2
photo_2.png

ASAMOAH EMMANUEL

unread,
Dec 5, 2024, 3:27:25 PM12/5/24
to django...@googlegroups.com
        from django.contrib import admin
        from django.urls import include, path

        urlpatterns = [
            path("", include("polls.urls")),
            path("admin/", admin.site.urls),

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/f09ef6d1-6e6a-4030-a122-c21e4939f821n%40googlegroups.com.


--
I don't stop when I'm tired, I only stop when the job is done.

Letlaka Tsotetsi

unread,
Dec 6, 2024, 1:17:41 AM12/6/24
to django...@googlegroups.com
Just from looking at your code structure I would assume the view is looking for the template to use when it renders your index view. 

Under your polls app normally you would need to add a folder named “templates” and under the template folder add another folder named “polls” and inside the polls folder add your index.html

It would also help to share your views.py code

Letlaka Tsotetsi
060 548 3215
Letl...@gmail.com


Otecina

unread,
Dec 6, 2024, 9:20:33 AM12/6/24
to django...@googlegroups.com

Change this code 👇

path("polls/", include("polls.urls")),

To
path("", include("polls.urls"),


--

Yunus Sezen

unread,
Dec 6, 2024, 9:20:50 AM12/6/24
to django...@googlegroups.com
Hello

Did you ever check installed apps?(settings.py)
The "polls" must be define into it

Thanks

5 Ara 2024 Per 18:52 tarihinde Олександр Рябов <rabo...@gmail.com> şunu yazdı:
--

Abduljelil Zubairu

unread,
Dec 6, 2024, 9:21:05 AM12/6/24
to Django users
is your VS Code on auto save 

Олександр Рябов

unread,
Dec 7, 2024, 3:34:29 PM12/7/24
to Django users
Guys, thanks for the help everyone but I just used vim and rewrote the code again)
Reply all
Reply to author
Forward
0 new messages