Home > Net >  Djnago : AttributeError: module 'django.views.generic' has no attribute 'Detail'
Djnago : AttributeError: module 'django.views.generic' has no attribute 'Detail'

Time:02-10

where can I import the detail attribute from?

views.py:

from django.shortcuts import render
from django.views import generic

from . import models

class Index(generic.TemplateView):

   template_name='catalog/index.html'

   def get_context_data(self, **kwargs):
       context = super().get_context_data(**kwargs)
       context['num_books'] = models.Book.objects.all().count()
       context['num_instances'] = models.BookInstance.objects.all().count()
       context['num_instances_available'] = models.BookInstance.objects.filter(status__exact='a').count()
       context['num_authors'] = models.Author.objects.count()

       return context

class BookListView(generic.ListView):

   model = models.Book
   template_name = 'catalog/book_list.html'

class BookDetialView(generic.Detail.View):

   model= models.Book
   template_name = 'catalog/book_detail.html'

urls.py

from django.urls import include, path, re_path
from . import views
from django.views.generic import TemplateView


app_name='catalog'
urlpatterns = [
   path(r'', views.Index.as_view(), name='index'),
   re_path(r'^$', views.Index.as_view(), name='index')
]

urlpatterns = [
   path(r'^$', views.index, name='index'),
   path(r'^books/$', views.BookListView.as_view(), name='books'),
   path(r'^book/(?P<pk>\d )$', views.BookDetailView.as_view(), name='book-detail'),
]

but the result:

class BookDetialView(generic.Detail.View):

AttributeError: module 'django.views.generic' has no attribute 'Detail'

CodePudding user response:

Should be generic.DetailViewinstead of generic.Detail.View

CodePudding user response:

In your BookDetailView you used generic.Detail.View instead of DetailView[Djano-doc].

Change your BookDetailView as

from django.views import generic

class BookDetialView(generic.DetailView): #<---  change here

   model= models.Book
   template_name = 'catalog/book_detail.html'

CodePudding user response:

You have a couple of issues here.

(1) In your views you spell your view name as BookDetialView and in your url definition you spell it BookDetailView.

(2) I believe the correct inheritance is generic.DetailView instead of generic.Detail.View

  • Related