Can django-pagination do multiple paginations per page?

2024/10/10 22:27:22

If it can't then are there any other alternatives (either Django's native pagination or an alternate package) that allows multiple paginations per page?

I would like to display a list of about 5 objects with each object having its own pagination context.

For convenience, here is the documentation for django-pagination.

Answer

I know this post is old, but it is still relevant. The following will work for Django 1.9.

This is how to do it,

views.py

def myview():Model_one = Model.objects.all()paginator = Paginator(Model_one, 6)page = request.GET.get('page1')try:Model_one = paginator.page(page)except PageNotAnInteger:Model_one = paginator.page(1)except EmptyPage:Model_one = paginator.page(paginator.num_pages)Model_two = Model_other.objects.all()paginator = Paginator(Model_two, 6)page = request.GET.get('page2')try:Model_two = paginator.page(page)except PageNotAnInteger:Model_two = paginator.page(1)except EmptyPage:Model_two = paginator.page(paginator.num_pages)context = {'Model_one': Model_one, 'Model_two': Model_two}return render(request, 'template.html', context)

The important thing above is the 'page1' and 'page2'.

In the template,

    {% if model_one %}<div class="col-md-12 well">{% for item in model_one %}..... iterates through model_one.....{% endfor %}<span class="step-links pagination">{% if model_one.has_previous %}<a href="?page1={{ model_one.previous_page_number }}"> previous </a>{% endif %}<span class="current">Page {{ model_one.number }} of {{ model_one.paginator.num_pages }}</span>{% if model_one.has_next %}<a href="?page1={{ model_one.next_page_number }}"> next </a>{% endif %}</span></div>{% endif %}{% if model_two %}<div class="col-md-12 well">{% for item in model_two %}..... iterates through model_two.....{% endfor %}<span class="step-links pagination">{% if model_two.has_previous %}<a href="?page2={{ model_two.previous_page_number }}"> previous </a>{% endif %}<span class="current">Page {{ model_two.number }} of {{ model_two.paginator.num_pages }}</span>{% if model_two.has_next %}<a href="?page2={{ model_two.next_page_number }}"> next </a>{% endif %}</span></div>{% endif %}

Again using 'page1' and 'page2' to distinguish the pagination for each model.

https://en.xdnf.cn/q/69847.html

Related Q&A

List directory file contents in a Django template

Im just learning Python & Django. (Thanks to everyone who contributes here -- its been an invaluable resource!)One seemingly basic thing that Im having trouble with is rendering a simple list of s…

Produce PDF files, draw polygons with rounded corners

Whats the right tool for the job if I want to write a Python script that produces vector graphics in PDF format? In particular, I need to draw filled polygons with rounded corners (i.e., plane figures…

How can I unit test this Flask app?

I have a Flask app that is using Flask-Restless to serve an API.I have just written some authentication that checksIf the consumers host is recognised The request includes a hash (calculated by encrypt…

How to completely reset Python stdlib logging module in an ipython session?

Id like to make repeated calls to Python scripts using %run in an ipython session, and for each of those scripts to log based on cmdline arguments passed via %run.For example while debugging cmd.py I m…

I think Librosa.effect.split has some problem?

firstly, this function is to remove silence of an audio. here is the official description:https://librosa.github.io/librosa/generated/librosa.effects.split.htmllibrosa.effects.split(y, top_db=10, *karg…

Replace None in list with leftmost non none value

Givena = [None,1,2,3,None,4,None,None]Id likea = [None,1,2,3,3,4,4,4]Currently I have brute forced it with:def replaceNoneWithLeftmost(val):last = Noneret = []for x in val:if x is not None:ret.append(x…

Generating lists/reports with in-line summaries in Django

I am trying to write a view that will generate a report which displays all Items within my Inventory system, and provide summaries at a certain point. This report is purely just an HTML template by the…

How to test if a view is decorated with login_required (Django)

Im doing some (isolated) unit test for a view which is decorated with "login_required". Example:@login_required def my_view(request):return HttpResponse(test)Is it possible to test that the &…

Filter Nested field in Flask Marshmallow

I want to filter the nested field with is_active column as True in Marshmallow 3 Consider following scenario I have 3 tablesusers (id, name) organizations (id, name) organization_user(id, organization_…

Copy signature, forward all arguments from wrapper function

I have two functions in a class, plot() and show(). show(), as convenience method, does nothing else than to add two lines to the code of plot() likedef plot(self,show_this=True,show_that=True,color=k,…