Mohamed Saad
Mohamed Saad

Reputation: 51

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

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

Upvotes: 5

Views: 6114

Answers (6)

varsha
varsha

Reputation: 1

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'

Upvotes: 0

vikram
vikram

Reputation: 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')

Upvotes: 0

vikram
vikram

Reputation: 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')

Upvotes: -1

Safarnejad
Safarnejad

Reputation: 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.

Upvotes: -1

osama abbas
osama abbas

Reputation: 9

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')

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

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.

Upvotes: 10

Related Questions