Thursday, December 5, 2024

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

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
Letlaka.t@gmail.com


On Thu, 05 Dec 2024 at 5:52 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/CAMCgpdKpQVGuHDKmd4hhCbBkvC7kxQux1bHXbLBPsSiNLkxUoQ%40mail.gmail.com.

No comments:

Post a Comment