AttributeError: type object 'AuthorDetailView' has no attribute 'as_views'

AttributeError: type object 'AuthorDetailView' has no attribute 'as_views'

我是 python-django 编码的初学者。目前我正在按照本指南为作者页面 (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views) 做具有挑战性的部分,但我面临一个问题 AttributeError: type object 'AuthorDetailView' has no attribute 'as_views'.

下面是我的代码: 在urls.py

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'),
    path('authors/', views.AuthorListView.as_view(), name ='authors'),
    path('authors/<int:pk>', views.AuthorDetailView.as_views(), name='author-detail'),
]

在views.py中:

from django.shortcuts import render

# Create your views here.
from .models import Book, Author, BookInstance, Genre

    def index(request):
        """View function for home page of site."""
    
        # Generate counts of some of the main objects
        num_books = Book.objects.all().count()
        num_instances = BookInstance.objects.all().count()
    
        # Available books (status = 'a')
        num_instances_available = BookInstance.objects.filter(status__exact='a').count()
    
        # The 'all()' is implied by default.
        num_authors = Author.objects.count()
    
        context = {
            'num_books': num_books,
            'num_instances': num_instances,
            'num_instances_available': num_instances_available,
            'num_authors': num_authors,
        }
    
        # Render the HTML template index.html with the data in the context variable
        return render(request, 'index.html', context=context)
    
    from django.views import generic
    
    class BookListView(generic.ListView):
        model = Book
        paginate_by = 5
    
    class BookDetailView(generic.DetailView):
        model = Book
    
    class AuthorListView(generic.ListView):
        model = Author
        paginate_by = 5
    
    class AuthorDetailView(generic.DetailView):
        model = Author

如果有人能帮助我,我将不胜感激。谢谢

你打错了。

as_view()不是as_views()

path('authors/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),

试试这个。