Thursday, December 5, 2024

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

Change this code 👇

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

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


On Thu, Dec 5, 2024, 4:51 PM Олександр Рябов <rabov1400@gmail.com> wrote:
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


#2

--
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+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/f09ef6d1-6e6a-4030-a122-c21e4939f821n%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/CANdWVZaVUeyX%3D_2veCSdDDcJR-wwi-TnnGnCgjuc9nftUhte%3Dw%40mail.gmail.com.

No comments:

Post a Comment