Generic views

Hi there, I am now in the genric views, and after implementing them I get an error saying:
module ‘polls/views’ has no atribute ‘index’
Note that polls/views.py used to have a function:
def index(parameter): ...
The tutorial asked to remove these function and subtitute them withblocks of code tha is charecerized with class.

I tried using the three backticks but didnt work, I dont know why

Carefully compare your views.py and urls.py files to the ones specified here: Writing your first Django app, part 4 | Django documentation | Django. The error module 'polls/views' has no attribute 'index' means that the previous url path path('', views.index, name='index'), is erroring at the point of views.index because index isn’t a method in the views.py file. This should be because you’ve replaced it with the class IndexView.

When I changed the urlpatterns to fit the views, now I am having error:
System check identified 4 errors.
On stack overflow it says the problem could from the database, but I dont know how to fix it

I do have a file called db.sqlite3
But when I ran the comand py manage.py dbshell I got: You appear not to have the ‘sqlite3’ program installed or on your path

Did you reactivate your virtual environment for the project before running manage.py dbshell?

I just did. That didnt help unfortunetly

These types of responses don’t allow for me/the community to help you. Please be specific about what you tried, what happened and what you expected to happen. This is in reference to the following messages:

If there is an exception, copy and paste the stacktrace and the full error message.

Yes you were right, I was not clear.
Before the error it asked me to write as_view() after each view urlspatterns in polls/urls
Like:
path('', views.IndexView.as_view(), name = 'IndexView'),

But now it is raising another error:
NoReverseMatch at /polls/ :
Reverse for ‘DetailView’ not found.
And it points to the link in the index.html template:

<li>
<a href = "{% url 'polls:detail' question.id %}>{{ question.question_text }}
</a> 
</li>

Thank you. The tutorial says to only change the view portion, not the names of the urls.

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

Rather than:
path('', views.IndexView.as_view(), name = 'IndexView')
It’s
path('', views.IndexView.as_view(), name = 'index')

I’d suggest keeping in line with what the tutorial recommends as it makes future issues easier to solve. Once you change those, make sure you clean up any references in {% url polls: ... %} or reverse("polls: ...") calls.

1 Like

Thanks! That really heleped.
Maybe I should practice some patience next time