blacklight
blacklight

Reputation: 130

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

Upvotes: 1

Views: 666

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

Upvotes: 2

dentemm
dentemm

Reputation: 6379

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

Upvotes: 0

Related Questions