Comments (5)
@imran-uk
You're correct that it's not enough to pass page_obj
alone to the template
When using Paginator in Django templates, you need to follow these steps.
-> In your view, create an instance of the Paginator class and use it to paginate your queryset.
from django.core.paginator import Paginator
def my_view(request):
items_per_page = 10 # Define the number of items per page
queryset = YourModel.objects.all()
paginator = Paginator(queryset, items_per_page)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
results_list = page_obj.object_list # Extract the list of objects for the current page
context = {
"page_obj": page_obj,
"results_list": results_list,
}
return render(request, 'your_template.html', context)
-> In your template, you can then iterate over the results_list
to display the items on the current page.
<ul>
{% for item in results_list %}
<li>{{ item.name }}</li>
<!-- Display other item attributes as needed -->
{% endfor %}
</ul>
->To create pagination controls, you can use the page_obj provided by the Paginator:
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current-page">{{ page_obj.number }}</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
from djangoproject.com.
Hey @imran-uk that's correct!
It's important to note that you can't simply pass page_obj to the context and expect it to work like in the ListView approach.
Instead, explicitly extract the object_list like this:
page_obj = paginator.get_page(page_number)
results_list = page_obj.object_list
Then, include both "page_obj" and "results_list" in your context:
{
"page_obj": page_obj,
"results_list": results_list,
}
In your template, iterate over results_list to display the paginated results. This clarification can save you from the frustration of not seeing results when mistakenly passing just page_obj to your template.
from djangoproject.com.
from djangoproject.com.
If this issua still exists should a new issue be opened in the main Django repository, since the documentation pages are generated by Sphinx from docs in django/django?
from djangoproject.com.
This is the issue tracker for the djangoproject.com website.
from djangoproject.com.
Related Issues (20)
- Ability to use images in site content
- Active promotion of community initiatives HOT 1
- Direct side feedback mechanism
- ./manage.py update_docs_and_index failing HOT 1
- Non-docs content enhancements from user research
- Improved teaching materials for non-expert users
- Possible confusion in the docs' tutorial 1 HOT 1
- aiomysql usage issue with Django HOT 1
- Discrepancy in Documentation for RemoteUserBackend Usage
- Site performance issues/outages HOT 3
- Typo in href in the template? HOT 1
- tried to fix po problem, tx client pull error HOT 3
- Request to Add "Python Niger Community Launch" Event to Local Django Communities List HOT 2
- Add a way to let people create requests to add community local events HOT 3
- Dashboard doesn't work HOT 6
- Website is not running. HOT 3
- Evaluating: capability.save() did not finish after 3.00 seconds. HOT 2
- .save() is not working HOT 1
- Search seems to give inaccurate results HOT 1
- Why doesn't `manage.py startapp` give us an option to automatically add the app to `INSTALLED_APPS`? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from djangoproject.com.