[Answer]-Django-voting — using two views for one webpage

1👍

You don’t have to use that; object_list is a built-in Django generic view. Generic being the keyword, because it’s merely intended to make it easier for you if your view conforms to a standard. Since, you need more from your view than object_list can provide, it’s time to throw it out and write your own view. You can use object_list as a guide for creating your own view, but there’s a lot of extraneous boilerplate code in it just to make it generic. Simplistically, the following is all you need:

def my_view(request, page=None):
    paginate_by = 15

    qs = LibraryRequest.objects.all()
    paginator = Paginator(qs, paginate_by, allow_empty_first_page=True)
    if not page:
        page = request.GET.get('page', 1)
    try:
        page_number = int(page)
    except ValueError:
        if page == 'last':
            page_number = paginator.num_pages
        else:
            # Page is not 'last', nor can it be converted to an int.
            raise Http404
    try:
        page_obj = paginator.page(page_number)
    except InvalidPage:
        raise Http404

    render_to_response('spicemodels/view_requests.html', {
        'request_list': page_obj.object_list,
        'paginator': paginator,
        'page_obj': page_obj,
        'is_paginated': page_obj.has_other_pages(),
    }, context_instance=RequestContext(request))

Leave a comment