Appreciate assistance with Django Tutorial Part 6: Generic List and Detail Views run-time error

Hello Community,
Enjoying the Django tutorial very much and wishing this community a success in their learning journeys.

I would be happy if you can assist regarding the Django Tutorial Part 6: Generic List and Detail Views

When I run the final step as suggested here by running py manage.py runserver I see the following error in console

File "C:\Users\....\locallibrary\catalog\urls.py", line 5, in <module>
path('books/', views.BookListView.as_view(), name='books'),
AttributeError: module 'catalog.views' has no attribute 'BookListView'

Content from the respective files

urls.py file

from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('books/', views.BookListView.as_view(), name='books'),
path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'),
]

views.py file (Partial Content)

 from django.views import generic

class BookListView(generic.ListView):
    model = Book
    
class BookDetailView(generic.DetailView):
    model = Book

I also have the book_list.html and book_details.html files created as mentioned and placed in the correct path locallibrary\catalog\templates\catalog

As you can see above there is clearly a BookListView defined in views.py file

Hi @iambhargav and welcome to the community :wave:

I don’t have any experience with Django, but the complete tutorial code is on MDN’s GitHub repo. Maybe you can use it to find the problem.

A shot in the dark:
Is the model you used define/imported? If there’s a problem with it maybe the view couldn’t be created in the first place.

Sorry, that I can’t give you a better answer :frowning:

Have a nice weekend,
Michael

Hello @mikoMK :wave: ,

Thanks for your quick reply. The model used is imported (from django.db import models). I will verify if there were any updates to/issues with it. I can check the complete code on Github but I’m guessing the related files (views, urls) will be much further along in the tutorial so some trial and error required to determine the exact file contents required at this stage for a successful execution.

Once again appreciate your enthusiasm,

Have a long weekend,
Bhargav

Yeah, those are the finished files from the end of the whole tutorial I guess.

What I meant was: Do you import the actual “Book” model in your “views.py” with from .models import Book?

Apologies for the late reply. I didn’t want to just keep asking questions without doing due diligence

Item1

Yes. In my views.py file I imported the ‘Book’ Model using

from .models import Book, Author, BookInstance, Genre

Item2

I’ve verified the finished files and they have the same content required until Part 6 (plus additional content)

Item 3
On checking Django documentation for Class-based generic views there is slight change in syntax for calling List & Detail Views compared to the tutorial in views.py file (although I doubt the change matters in this case)

In Django tutorial it is:

from django.views import generic
class BookListView(generic.ListView):
model = Book

In Django documentation it is:

from django.views.generic import ListView
class BookListView(ListView):
model = Book

In spite of trying the above hacks still seeing the same error

It’s a pity it still isn’t working after all this effort. :slightly_frowning_face:

I agree. That shouldn’t make a difference. It just imports one level deeper and then uses ListView directly.

It’s possible that something fundamentally changed in newer Python/Django versions which makes the current tutorial code break. If you’re still interested in finding the cause of the problem you could use the complete code from GitHub in another folder and see if it works with a current version of Python/Django. If that fails it’s probably time to open an issue on GitHub to let someone update the code.

Maybe it’s generally a good idea to open an issue there with all the information you collected. Chances are the person updating the repo can push you in the right direction. As I initially said I know nothing about Python and it seems there’s no one else here knowledgeable about it either. :slightly_frowning_face:

I still wish you a nice weekend, though. :slightly_smiling_face:

Have you figured this out yet? I am currently struggling with this as well and have been trying to solve the issue for hours.

Nope! :neutral_face: Continued on to the next tutorial!