ImproperlyConfigured DetailView is missing a QuerySet

Total
11
Shares

I’m trying to use a generic DetailView for my model and I’m getting the error: ClassroomDetailView is missing a QuerySet. Define ClassroomDetailView.model, ClassroomDetailView.queryset, or override ClassroomDetailView.get_queryset(). I’ve seen other people with a similar error and the solution is typically that their url pattern didn’t properly reference their view name. I can’t find any typos like that for mine though. My understanding is that the generic DetailView does not require a querset.

models.py
class Classroom(models.Model):
    classroom_name = models.CharField(max_length=10)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

    def __str__(self):
        return self.classroom_name

views.py
class ClassroomDetailView(generic.DetailView):
    model: Classroom

urls.py
app_name = 'gradebook'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('signup/', SignUpView.as_view(), name='signup'),
    path('courses/', views.courses, name='courses'),
    path('classroom/', views.classroom, name='classroom'),
    path('classroom/<int:pk>/',
         views.ClassroomDetailView.as_view(), name='classdetail'),
    path('addstudent/<int:classroom_id>/', views.addstudent, name='addstudent'),
    path('addmultistudent/<int:classroom_id>/',
         views.addmultistudent, name='addmultistudent'),
    path('objective/<int:course_id>/', views.addobjective, name='addobjective'),
    path('addassessment/<int:course_id>/',
         views.addassessment, name='addassessment'),
]


Solution

You need to use "=" instead of ":":

# views.py
class ClassroomDetailView(generic.DetailView):
    model = Classroom
Leave a Reply

Your email address will not be published. Required fields are marked *