ListView is missing a QuerySet. Define ListView.model, ListView.queryset, or override ListView.get_queryset()
Asked Answered
F

6

5

I don't really understand what causes the error i checked the documentations and there was a very similar example of this one here is my views.py, urls.py under my app i use include, and the template

views.py

class SchoolListView(ListView):
    context_object_name = 'schools'
    model = models.School

urls.py

from django.urls import path
from . import views

#My name space
app_name = 'basicapp'
urlpatterns = [
    path('', views.ListView.as_view(), name='list'),
    path('details', views.DetailView.as_view(), name='details')
]

and my template

{% extends 'basicapp/basicapp_base.html'%}
{% block body_block %}
  <div class="jumbotron">
    <h1>Welcome to list of all schools</h1>
    <ol>
      {% for school in schools %}
      <h2><li><a href="{{school.id}}">{{school.name}}</a></li></h2>
      {% endfor %}
    </ol>

{% endblock %}

And i get this error which i don't really understand

Exception Type: ImproperlyConfigured
Exception Value:    
ListView is missing a QuerySet. Define ListView.model, ListView.queryset, or override ListView.get_queryset().


Traceback Switch to copy-and-paste view
C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\exception.py in inner
            response = get_response(request) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\base.py in view
            return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\base.py in dispatch
        return handler(request, *args, **kwargs) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\list.py in get
        self.object_list = self.get_queryset() ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\list.py in get_queryset
                    'cls': self.__class__.__name__ ...
▶ Local vars
Foreignborn answered 26/2, 2019 at 20:24 Comment(0)
S
10

There is an error in your urls.py, you did not refer to the SchoolListView, but to the generic ListView itself. You can fix this by writing:

# app/urls.py

from django.urls import path
from . import views

#My name space
app_name = 'basicapp'

urlpatterns = [
    # SchoolListView instead of ListView
    path('', views.SchoolListListView.as_view(), name='list'),
    # probably SchoolDetailView instead of DetailView, and with a pk in the url
    path('details', views.DetailView.as_view(), name='details')
]

Since you imported the ListView in your views.py, the interpreter does not error on using views.ListView, you simpy "re-exported" the ListView in your views.py.

Probably you also defined SchoolDetailView instead of DetailView, and likely the URL should contain the primary key of the school for which you want to show the details, but you did not provide sufficient code to solve that problem.

Sennar answered 26/2, 2019 at 20:34 Comment(0)
A
0

views.py should be:

class SchoolListView(ListView):
    context_object_name = 'schools'
    models = models.School

    def get_queryset(self):
        """Return Schools """
        return models.School.objects.order_by('id')
Airburst answered 23/9, 2019 at 14:25 Comment(0)
R
0
from .models import School

class SchoolListView(ListView):
    model = School

or try it also:

class SchoolListView(ListView): 
    model : School

    def get_queryset(self): 
        return School.objects.order_by('id')
Rogerson answered 27/7, 2022 at 7:57 Comment(0)
I
0
ImproperlyConfigured :Error

ListView is missing a QuerySet. Define ListView.model, ListView.queryset, or override ListView.get_queryset().
from django.http import HttpResponse
from datetime import datetime
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView

# class base views
class HomeView(TemplateView):
    template_name = 'wc.html'
    extra_context = {'today':datetime.today()}
class AuthorizedView(LoginRequiredMixin, TemplateView):
    template_name = 'authorized.html'
    login_url = '/admin'
Initiative answered 13/10, 2022 at 20:17 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Brunel
@varsha, use triple backticks to wrap code blocks for readability. other than that, please explain how your code solves the problem.Unreality
F
-1

First, you should add a get_queryset function to your SchoolListView Class like this:

def get_queryset(self): return models.School.objects.order_by('id')

then replace ListView in views.ListView.as_view() (in urls.py) with SchoolListView.

Flagstone answered 19/4, 2020 at 17:1 Comment(0)
R
-1
from .models import School

class SchoolListView(ListView):
    model = School

or try it also:

class SchoolListView(ListView):
    model = School 

 def get_queryset(self):
      return School.objects.order_by('id')
Rogerson answered 27/7, 2022 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.