I have tried something like this in views.py:
class HomePage(TemplateView):
template_name = "clouderp/index.html"
def get_context_data(self, **kwargs):
context = super(HomePage, self).get_context_data(**kwargs)
qs = Blog.objects.all()
context['blog_list'] = qs
page = self.request.GET.get('page')
paginator = Paginator(qs, 4)
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
context['users'] = users
return context
And in template:
{% if users.has_other_pages %}
<ul class="pagination">
{% if users.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in users.paginator.page_range %}
{% if users.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only"></span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if users.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
I have created a blog application using django
I want to show all my blogs in my main index.html page and also wanted to do pagination for the blogs in my index page...
I was wondering how to do it...
Because the process I have done the blogs are not paginated according to 4 at a time...
Please try this code as below:
in views.py code
class BookListView(ListView):
model=Book
paginate_by=10
template_name='book/book_list.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
print (context)
paginator = context['paginator']
page_numbers_range = 10 # Display 5 page numbers
max_index = len(paginator.page_range)
page = self.request.GET.get('page')
print (self.request)
current_page = int(page) if page else 1
start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
end_index = start_index + page_numbers_range
if end_index >= max_index:
end_index = max_index
page_range = paginator.page_range[start_index:end_index]
context['page_range'] = page_range
return context
book_list = BookListView.as_view()
and the template:
<div class="container">
<!-- Pagination -->
{% if is_paginated %}
<nav>
<ul class="pagination justify-content-center" style="margin:20px 0">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">
<span>Prev</span>
</a>
</li>
{% else %}
<li class="disabled page-item">
<a class="page-link" href="#">
<span>Prev</span>
</a>
</li>
{% endif %}
{% for page in page_range %}
<li {% if page == page_obj.number %} class="active page-item" {% endif %}>
<a class="page-link" href="?page={{ page }}">{{ page }}</a>
</li>
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}">
<span>Next</span>
</a>
</li>
{% else %}
<li {% if not page_obj.has_next %}class="disabled page-item"{% endif %}>
<a class="page-link" href="#">
<span>Next</span>
</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
Related
The paginator in django correctly calculates the number of pages, but does not break the models into pages and everything is displayed on 1 page, please tell me what to do with this
This is my views.py,where is a paginator:
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import *
def index(request):
news = Content.objects.filter(is_published=True)
page = request.GET.get('page', 1)
paginator = Paginator(news, 20)
try:
page_obj = paginator.page(page)
except EmptyPage:
page_obj = paginator.page(1)
except PageNotAnInteger:
page_obj = paginator.page(paginator.num_pages)
return render(request, 'content/index.html', {'news': news, 'page_obj': page_obj})
def view_news(request, news_id):
news_item = get_object_or_404(Content, id=news_id)
return render(request, 'Content/view_news.html', {'news_item': news_item})
And there is my paginator template:
{% block content %}
<nav class="paginator">
<ul class="pagination">
{% if page_obj.has_previous %}
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">
<li class="page-item">Предыдущая</li>
</a>
{% else %}
<a class="page-link">
<li class="page-item">Предыдущая</li>
</a>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<a class="page-link">
<li class="page-item">{{ i }}</li>
</a>
{% else %}
<a class="page-link" href="?page={{ i }}">
<li class="page-item">{{ i }}</li>
</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="page-link" href="?page={{ page_obj.next_page_number }}">
<li class="page-item">Следующая</li>
</a>
{% else %}
<a class="page-link">
<li class="page-item">Следующая</li>
</a>
{% endif %}
</ul>
</nav>
{% endblock %}
Help me please with this problem
I am filtering my data, and also applying paginator, I have found some solution and I used it but it is working only for the next button and not for previous and button numbers in between i.e. for {{i}} (Django, python)
this is template_tag file
myapp_extra.py
from django import template
register = template.Library()
#register.simple_tag
def my_url(value, field_name, urlencode=None):
url = '?{}={}'.format(field_name, value)
if urlencode:
querystring = urlencode.split('&')
filterd_querystring = filter(lambda p: p.split('=')[0]!=field_name, querystring)
encoded_querystring = '&'.join(filterd_querystring)
url = '{}&{}'.format(url, encoded_querystring)
return url
in my html
{% load myapp_extra %}
<other code>
<nav>
<ul class="pagination rounded-separated justify-content-center" style="float: right; margin-top: 1%;">
{% if employees_attendance_list.has_previous %}
<li class="page-item"><a class="page-link" href="{% my_url employees_attendance_list.previous_page_number 'page' request.GET.urlencode }"><i class="mdi mdi-chevron-left"></i></a></li>
{% else %}
<li class="disabled"></li>
{% endif %}
{% for i in employees_attendance_list.paginator.page_range %}
{% if employees_attendance_list.number == i %}
<li class="page-item active"><a class="page-link" >{{ i }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% my_url i 'page' request.GET.urlencode}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if employees_attendance_list.has_next %}
<li class="page-item"><a class="page-link" href="{% my_url employees_attendance_list.next_page_number 'page' request.GET.urlencode %}"><i class="mdi mdi-chevron-right"></i></a></li>
{% else %}
<li class="disabled"></li>
{% endif %}
</ul>
</nav>
in views
page = request.GET.get('page', 1)
paginator = Paginator(employees_attendance, settings.PAGE_LIMIT)
try:
employees_attendance_list = paginator.page(page)
except PageNotAnInteger:
employees_attendance_list = paginator.page(1)
except EmptyPage:
employees_attendance_list = paginator.page(paginator.num_pages)
I have made some changes in my html
<nav>
<ul class="pagination rounded-separated justify-content-center" style="float: right; margin-top: 1%;">
{% for i in employees_attendance_list.paginator.page_range %}
<li class="page-item">
<a class="page-link" href="{% my_url i 'page' request.GET.urlencode %}">{{ i }}</a>
</li>
{% endfor %}
</ul>
</nav>
So I'm trying to use pagination in django, When I click on previous it takes me to localhost:8000/thatpage/?page= when it is supposed to take me to the previous page, It also gives me error on that page. I have tried handling it in my views.py but it doesn't work.
I have also tried using books_obj.previous_page_number but that doesn't work too.
I'm using Django 3.1
views.py
def showproducts(request):
oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
hehe = CartItem.objects.filter(user=request.user)
category = Category.objects.all()
haha = Paginator(Book.objects.all(), 2)
page = request.GET.get('page')
if page is None or "":
page = 1
fianlprice = 0
for item in hehe:
fianlprice += item.book.price
# books = Book.objects.all()
books = haha.page(page)
return render(request, 'main/products.html', {'books':books, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})
products.html
<h1>Products</h1>
<h1>{{ error }}</h1>
{% if user.is_authenticated %}
<h1>Your cart currently costs ${{ price }}</h1>
{% else %}
<h1>Please login to view your cart</h1>
{% endif %}
<form method="GET" action="/search/">
<label>Choose a category</label>
<select name="category" id="category">
<option value="All" selected>All</option>
{% for name in category %}
<option value="{{ name.name }}">{{ name.name }}</option>
{% endfor %}
</select>
<input type="text" placeholder="Search here" name="search" id="search">
<button type="submit">Search</button>
</form>
{% for book in books %}
<h3>{{ book.name }}</h3>
<img src= "/media/{{ book.image }}" alt="">
<p>{{ book.description }}</p>
{% if not user.is_authenticated %}
<p>Please login</p>
{% else %}
{% if book.id in cart %}
<form method="POST" action="/removefromcartforhome/">
{% csrf_token %}
<button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>
</form>
{% elif book.id in order %}
<h3>You already own this</h3>
{% else %}
<form method="POST" action="/addtocartforhome/">
{% csrf_token %}
<button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>
</form>
{% endif %}
{% endif %}
{% endfor %}
{% if books.has_other_pages %}
{% if books.has_previous %}
<li class="page-item">
«
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</a>
</li>
{% endif %}
{% for i in books.paginator.page_range %}
{% if books.number == i %}
<li class="page-item active">
<a class="page-link">{{i}}</a>
</li>
{% else %}
<li class="page-item">
{{i}}
</li>
{% endif %}
{% endfor %}
{% if books.has_next %}
<li class="page-item">
»
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">»</a>
</li>
{% endif %}
{% else %}
<p>no pages</p>
{% endif %}
Change this code from:
if page is None or "":
page = 1
to
page = request.GET.get('page', None)
if page == None or page == "":
page = 1
# or
if not page:
page = 1
Update
You can use http referee to redirect to previous page:
if not page:
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
I'm trying to paginate, it shows in the address bar that I'm on page 2 but nothing new gets displayed.
Below is an excerpt from my views.py looks:
class JobList(ListView):
model = Job
template_name = "jobs/job_listings.html"
context_object_name = "job_list"
ordering = ['-published_date']
paginate_by = 3
def get_context_data(self, **kwargs):
context = super(JobList, self).get_context_data(**kwargs)
jobs_expiring_soon = Job.objects.filter(application_last_date__gte=datetime.now(), application_last_date__lte=lookup_date)
state_list = State.objects.order_by('name')
profession_list = Profession.objects.order_by('name')
context['jobs_expiring_soon'] = jobs_expiring_soon
context['state_list'] = state_list
context['profession_list'] = profession_list
return context
Below is an excerpt from my urls.py file:
path('', JobList.as_view(), name = 'job-list'),
Below is the associated template:
{% extends 'base.html' %}
{% block page_content %}
{% for job in job_list %}
<div class="listing-wrapper">
<div class="listing-container border-top border-bottom">
<a href="{{ job.get_absolute_url }}">
<h2 class="heading mt-3 mb-1 mx-2 d-inline-block">{{ job.title|truncatechars:75 }}</h2>
<p class="mx-2"><span class="sub-heading mr-1">Number of Posts:</span><span class="mr-1 ml-1">{{ job.nop }}</span>|<span class="sub-heading ml-1 mr-1">Last Date to Apply:</span><span>{{ job.application_last_date|date:"j-M-Y" }}</span></p>
<p class="mx-2 mb-3">{{ job.summary|truncatechars:200 }}</p>
</a>
</div>
</div>
{% endfor %}
{% if is_paginated %}
<ul class="pagination justify-content-center my-4">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link bg-dark text-white" href="?page{{ page_obj.previous_page_number }}">← Previous Page</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link bg-dark text-white" href="?page{{ page_obj.next_page_number }}">Next Page →</a>
</li>
{% endif %}
</ul>
{% endif %}
{% endblock page_content %}
I would really grateful, if anyone could please help me in getting this fixed. Thanks in advance!
Try setting context_object_name = 'job'. I believe _list gets appended to it automatically which would mean your current view generates the context job_list_list.
I'm trying to set up pagination within my Django project but I can't find a way to make the filters (ptags in my case) follow to the next pages.
Ps; I use Django-haystack with faceting for filtering.
I have a custom forms.py
from haystack.forms import FacetedSearchForm
class FacetedProductSearchForm(FacetedSearchForm):
def __init__(self, *args, **kwargs):
data = dict(kwargs.get("data", []))
self.ptag = data.get('ptags', [])
super(FacetedProductSearchForm, self).__init__(*args, **kwargs)
def search(self):
sqs = super(FacetedProductSearchForm, self).search()
if self.ptag:
query = None
for ptags in self.ptag:
if query:
query += u' OR '
else:
query = u''
query += u'"%s"' % sqs.query.clean(ptags)
sqs = sqs.narrow(u'ptags_exact:%s' % query)
return sqs
That I pass in my Views:
class FacetedSearchView(BaseFacetedSearchView):
form_class = FacetedProductSearchForm
facet_fields = ['ptags']
template_name = 'search_result.html'
paginate_by = 3
context_object_name = 'object_list'
Here's my full search_result.html:
<div>
{% if page_obj.object_list %}
<ol class="row top20" id="my_list">
{% for result in page_obj.object_list %}
<li class="list_item">
<div class="showcase col-sm-6 col-md-4">
<div class="matching_score"></div>
<a href="{{ result.object.get_absolute_url }}">
<h3>{{result.object.title}}</h3>
<h5>{{ result.object.destination }}</h5>
<img src="{{ result.object.image }}" class="img-responsive">
</a>
<div class="text-center textio">
<ul class="tagslist">
<li class="listi">
{% for tags in result.object.ptags.names %}
<span class="label label-info"># {{ tags }}</span>
{% endfor %}
</li>
</ul>
</div>
</div>
</li>
{% endfor %}
</ol>
</div>
I'm able to pass the query which is a destination to the next page by adding this at the bottom of my html:
{% if is_paginated %}
<ul class="pagination pull-right">
{% if page_obj.has_previous %}
<li><i class="fas fa-angle-left"></i>Previous page</li>
{% else %}
<li class="disabled"><span><i class="fas fa-angle-left"></i>Previous page</span></li>
{% endif %}
{% if page_obj.has_next %}
<li>See more results<i class="fas fa-angle-right"></i></li>
{% else %}
<li class="disabled"><span>See more results<i class="fas fa-angle-right"></i></span></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<p> Sorry, no result found for the search term <strong>{{query}} </strong></p>
{% endif %}
But If I try to add the ptags field within the pagination code by doing &ptags like this:
{% if is_paginated %}
<ul class="pagination pull-right">
{% if page_obj.has_previous %}
<li><i class="fas fa-angle-left"></i>Previous page</li>
{% else %}
<li class="disabled"><span><i class="fas fa-angle-left"></i>Previous page</span></li>
{% endif %}
{% if page_obj.has_next %}
<li>See more results<i class="fas fa-angle-right"></i></li>
{% else %}
<li class="disabled"><span>See more results<i class="fas fa-angle-right"></i></span></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<p> Sorry, no result found for the search term <strong>{{query}} </strong></p>
{% endif %}
I get this error when I go to page 2 and the selected checkboxes filters (ptags) are not following:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/search/?q=las%20vegas&ptags=&page=2
Raised by: search.views.FacetedSearchView
How can I fix this?
Your template pagination code is removing the extra filter parameters, use this -
In your my_tags.py add this template tag:
#register.simple_tag(name='url_replace')
def url_replace(request, field, value):
d = request.GET.copy()
d[field] = value
return d.urlencode()
paginate as -
{% if is_paginated %}
<br>
<div class="row">
<div class="col-12">
<ul class="pagination float-right">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?{% url_replace request 'page' page_obj.previous_page_number %}">«</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active page-item"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item"><a class="page-link" href="?{% url_replace request 'page' i %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?{% url_replace request 'page' page_obj.next_page_number %}">»</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">»</span></li>
{% endif %}
</ul>
</div>
</div>
{% endif %}