Comments (4)
@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.
Related Issues (20)
- New content: Accessibility statement HOT 1
- Many company logos on the fundraiser page are unreadable in dark mode HOT 1
- django-debug-toolbar audit and accessibility review
- When the "hamburger" menu is open, pressing the tab key for the first time should bring the focus to the first item in the menu list
- Upgrade Django from 3.2 to 4.2 HOT 7
- Donation not working: "There was an error setting up your donation. Sorry. Please refresh the page and try again." HOT 3
- Incorrect URL on the Django Software Foundation Prizes page HOT 12
- Ticket Tracker UI
- bulk_create example code provided seems to go into infinite loop HOT 3
- Code block indentation on /start page HOT 1
- Site version picker rendering link text as tuples with identical `href` attributes; stuck on 5.0 without URL changes HOT 1
- Styleguide page for content formatting options
- Help text for foundation Meeting model fields HOT 1
- Foundation model changes for meeting minutes
- Update fundraising counter footnote to explain running total and goal
- Docker Setup: docker-entrypoint.sh File Overwritten with Windows Line Endings
- Remove "Django-powered Sites" from Community sections HOT 2
- Add ids to "Local Django Communities" page HOT 2
- Animated icons do not respect prefers-reduced-motion HOT 1
- Many docs.djangoproject.com links fail to load with 502 Bad Gateway errors HOT 1
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.