1

I have an Asset Detail View that uses data from another two models, Tenant and Service.

asset\views.py

class AssetMultipleDetailView(LoginRequiredMixin, UserPassesTestMixin, DetailView):
    model = Asset
    context_object_name = 'asset'
    template_name = 'asset/asset_multiple_detail.html'

    def test_func(self):
        asset_multiple = self.get_object()
        if self.request.user == asset_multiple.owner:
            return True
        return False

    def get_context_data(self, **kwargs):
        context = super(AssetMultipleDetailView, self).get_context_data(**kwargs)
        context['tenants'] = Tenant.objects.filter(asset=context['asset']).order_by('created')
        context['services'] = Service.objects.filter(asset=context['asset']).order_by('created')
        return context

I would like to paginate the data that's in context['tenants] and context['services'] and I do not know how to achieve this.

I have done this in their own ListViews using paginated_by and got it working without any issues.

I simply would like to know how to paginate a context in a DetailView.

Thank you in advance.

Nadun Perera
  • 565
  • 6
  • 15
  • Check this out: https://stackoverflow.com/a/53491725/3345051 – Hybrid Apr 29 '19 at 01:41
  • Thank you Hybrid. I saw that one earlier. But it refers to the Partner model in the Partner DetailView. In my case, I am referring to the Tenant and the Service model in Asset DetailView. – Nadun Perera Apr 29 '19 at 02:10
  • [DataTables](https://datatables.net/) makes pagination as easy as it can get. On any kind of _HTML_ table. – Love Putin Not War Mar 14 '22 at 12:23

1 Answers1

2

You can follow the example provided in django's example on putting pagination in a view. Like this:

class AssetMultipleDetailView(LoginRequiredMixin, UserPassesTestMixin, DetailView):
    ...

    def get_context_data(self, **kwargs):
        context = super(AssetMultipleDetailView, self).get_context_data(**kwargs)
        page = request.GET.get('page')
        tenants = paginator = Paginator(Tenant.objects.filter(asset=self.get_object()).order_by('created'), 5)  # will show 5 items in one page
        services = Paginator(Service.objects.filter(asset=self.get_object()).order_by('created'), 5)
        context['tenants'] = tenants.get_page(page)
        context['services'] = services.get_page(page)
        return context

And update the template like this:

// tenants pages
<div class="pagination">
    <span class="step-links">
        {% if tenants.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ tenants.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ tenants.number }} of {{ tenants.paginator.num_pages }}.
        </span>

        {% if tennats.has_next %}
            <a href="?page={{ tenants.next_page_number }}">next</a>
            <a href="?page={{ tenants.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

// services
<div class="pagination">
    <span class="step-links">
        {% if services.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ services.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ services.number }} of {{ services.paginator.num_pages }}.
        </span>

        {% if services.has_next %}
            <a href="?page={{ services.next_page_number }}">next</a>
            <a href="?page={{ services.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>
ruddra
  • 50,746
  • 7
  • 78
  • 101