[Answered ]-How do I convert a view method to a Generic List View?

2👍

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 %}

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

Leave a comment