Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
How do I convert a view method to a Generic List View?

I have this method on the view page. It works fine and shows everything correctly but I want to convert it into a generic list view so that I could apply pagination to it.

Here is the function :`

#views.py
def index(request):
    all_artists = Artist.objects.all()
    all_songs = Song.objects.all()
    all_albums = Album.objects.all()

    return render(request, 'index.html', {'all_albums':all_albums,'all_songs':all_songs, 'all_artists':all_artists})

So I followed some tutorials and ended up with this:

#new views.py
class IndexView(ListView):
    template_name = 'index.html'
    context_object_name = 'home_list'
    queryset = Artist.objects.all()

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['all_artists']=Artist.objects.all()
        context['all_songs']=Song.objects.all()
        context['all_albums']=Album.objects.all()

Though it compiles without any error, when I render the page, the context object does not gets rendered. Your help is very much appreciated! Thankyou

EDIT(13-APR-17): Thankyou guys! It finally worked with some little modifications.

class IndexView(generic.ListView):
    template_name = 'index.html'
    context_object_name = 'home_list'
    queryset = Artist.objects.all()

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['all_artists']=Artist.objects.all()
        context['all_songs']=Song.objects.all()
        context['all_albums']=Album.objects.all()  
        return context
    enter code here
    enter code here
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

urls.py in urls.py from your django app you need to include a url that references to your views and include this urls.py to your django project main urls.py.

#urls.py
from django.conf.urls import url
from .views import IndexView

urlpatterns = [
url(r'^path/$', IndexView.as_view(), name="index"),
]

Then in your views.py override the variable paginate_by

#views.py
class IndexView(ListView):
    template_name = 'index.html'
    context_object_name = 'home_list'
    queryset = Artist.objects.all()
    paginate_by = 10 # Number of objects for each page

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['all_artists']=Artist.objects.all()
        context['all_songs']=Song.objects.all()
        context['all_albums']=Album.objects.all()  
        return context

Finally in your index.html add the pagination {% pagination_for page_obj %}

{% block content %}

<!--some content -->

<!--begin paginator -->
{% pagination_for page_obj %}
<!--end paginator-->

{% endblock %}
about 4 years ago · Santiago Trujillo Report

0

Your get_context_data() needs to return the context. So if this is your exact code, you need to add return context to it

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!