My paginator won't show the number of pages - python

for some reason my paginators not working properly on my post_list page, but it works for my tags_list_page and its almost identical. It wont display the number of pages. Heres my code for them
post list.html pagination
<div class="text-center" style="margin-bottom: 20px">
<ul class="pagination">
{% if object_list.has_previous %}
<li><<</li>
<li><a href="?{{ page_request_var }}={{ object_list.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">prev</a></li>
{% endif %}
{% for i in paginator.page_range %}
<li {% if page_obj.number == i %} class="active" {% endif %}>{{i}}<li>
{% endfor %}
{% if object_list.has_next %}
<li><a href="?{{ page_request_var }}={{ object_list.next_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a></li>
<li>>></li>
{% endif %}
</ul>
tags list.html pagination
<div class="text-center" style="margin-bottom: 20px">
<ul class="pagination">
{% if queryset.has_previous %}
<li><<</li>
<li><a href="?{{ page_request_var }}={{ queryset.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">prev</a></li>
{% endif %}
{% for i in paginator.page_range %}
<li {% if page_obj.number == i %} class="active" {% endif %}>{{i}}<li>
{% endfor %}
{% if queryset.has_next %}
<li><a href="?{{ page_request_var }}={{ queryset.next_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a></li>
<li>>></li>
{% endif %}
</ul>
my views
def post_list(request):
today = timezone.now().date()
queryset_list = Post.objects.active()
if request.user.is_staff or request.user.is_superuser:
queryset_list = Post.objects.all()
paginator = Paginator(queryset_list, 8)
page_request_var = 'page'
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
template = "posts/post_list.html"
name = "user"
count = queryset_list.count()
context = {
"object_list": queryset,
"name": name,
"page_request_var": page_request_var,
"today": today,
"count": count
}
return render(request, template, context)
def tag_list(request, slug=None):
today = timezone.now().date()
instance = get_object_or_404(Tag, slug=slug)
ins = instance.post_set.all()
queryset_list = Post.objects.active()
if request.user.is_staff or request.user.is_superuser:
queryset_list = ins
paginator = Paginator(queryset_list, 9)
page_request_var = "tags"
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
hey = paginator.num_pages
kount = queryset_list.count()
name = "Tags list"
context = {
"queryset": queryset,
"paginator": paginator,
"page_request_var": page_request_var,
"hey": hey,
"title": "posts",
"name": name,
"today": today,
"kount": kount
}
return render(request, "posts/tag_list.html", context)
It works for the tags pagination fine as I said. dont know whats going on
It was working fine. Any help is appreciated

You're not passing the paginator to the template in the post_list view.

Related

pagination in django works calculates pages correctly but doesn't separate them

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

Django navbar links disappearing in the post

So sorry probably this is very easy but i learn django and have a problem. My navbar working on index page is perfectly. but when i go contact form or another post url, navbar links disappear.
When i go some post page or my contact form page
1- {{ article.title }} and href links are disappear
Can i ask help ?
my navbar.html
{% load i18n %}
<!-- Menu -->
<div class="menu-wrapper center-relative">
<nav id="header-main-menu">
<div class="mob-menu">Menu</div>
<ul class="main-menu sm sm-clean">
<li>{% trans "HomePage" %}</li>
<li>{% trans "Services" %}</li>
<li>
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
</li>
<li>{% trans "HELLO WORLD" %}</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1643 dropdown">
<a title="" href="">{% trans "Producs" %}</a>
<ul role="menu" class=" dropdown-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644 dropdown">
<a title="Level 2" href="">{% trans "Consult" %}</a>
<ul role="menu" class=" dropdown-menu">
<li >
{% for category in category %}
{% if category.name == 'consult' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}</li>
</ul>
</li>
<li>
{% for category in category %}
{% if category.name == 'header' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
</li>
<li>{% trans "İletişim" %}</li>
</ul>
</nav>
</div>
article/views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
from django.core.paginator import Paginator
from django.utils.translation import gettext as _
# Create your views here.
def articles(request):
keyword = request.GET.get("keyword")
if keyword:
articles = Article.objects.filter(title__contains=keyword)
paginator = Paginator(articles, 1)
page = request.GET.get('page')
articles = paginator.get_page(page)
return render(request,"articles.html",{"articles":articles})
articles = Article.objects.all()
paginator = Paginator(articles, 10)
page = request.GET.get('page')
articles = paginator.get_page(page)
return render(request, "articles.html", {"articles":articles})
def index(request):
articles = Article.objects.all()
category = Category.objects.all()
context = {
"articles": articles,
"category": category,
}
return render(request, 'index.html', context)
def detail(request,slug):
# article = Article.objects.filter (id = id).first()
article = get_object_or_404(Article, slug = slug)
category = Category.objects.all()
return render(request, "detail.html", {"article":article, "category":category,})
def category_detail(request,slug):
template = "category_detail.html"
category=get_object_or_404(Category,slug=slug)
article=Article.objects.filter(category=category)
context = {
'category' : category,
'article' : article,
}
return render(request,template,context)
def category_page(request):
object_list = Category.objects.all()
context = {'object_list': object_list,}
return render(request, 'detail.html', context)
contact_form/views.py
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')

Pagination in Django not working as expected

I wanted pagination for all the courses available and that was easy to achieve. But now I'm stuck because I wanted pagination for faculties also, which will show specific courses of the accessed faculty. I have 4 models: faculties, departments, studies, and courses. The pagination will show for faculties as well, but the problem is that if I try to go to the second page, it will redirect me to the second page of all courses list. Or, if I change page on all courses and then try to access a faculty, no course will show at all in the faculty.
def index(request):
course_list = Course.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(course_list, 1)
try:
courses = paginator.page(page)
except PageNotAnInteger:
courses = paginator.page(1)
except EmptyPage:
courses = paginator.page(paginator.num_pages)
faculty_list = Faculty.objects.all()
page = request.GET.get('page2', 1)
paginator = Paginator(faculty_list, 1)
try:
faculties = paginator.page(page)
except PageNotAnInteger:
faculties = paginator.page(1)
except EmptyPage:
faculties = paginator.page(paginator.num_pages)
context = {'courses': courses,
'faculties': faculties,
'departments': Department.objects.all(),
'studies': StudyProgramme.objects.all(),
'teachers': Teacher.objects.all()
}
return render(request, 'courses/index.html', context)
<div id="crs">
<h3>All courses</h3>
<ul>
{% for course in courses %}
<li><a href={{ course.slug }}>{{ course.name }}</a></li>
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if courses.has_previous %}
« first
{{ courses.previous_page_number }}
{% endif %}
<span class="current">
Page {{ courses.number }} of {{ courses.paginator.num_pages }}
</span>
{% if courses.has_next %}
{{ courses.next_page_number }}
last »
{% endif %}
</span>
</div>
</div>
{% for faculty in faculties %}
<div id="fac_{{ faculty.pk }}_tab" style="display:none;">
<h3> {{ faculty.name }} Courses</h3>
<ul>
{% for department in faculty.department_set.all %}
{% for study in studies %}
{% if study.department == department %}
{% for course in courses %}
{% if course.study_programme == study %}
<li>
<a class="first" href={{ course.slug }}>{{ course.name }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if faculties.has_previous %}
« first
{{ faculties.previous_page_number }}
{% endif %}
<span class="current">
Page {{ faculties.number }} of {{ faculties.paginator.num_pages }}
</span>
{% if faculties.has_next %}
{{ faculties.next_page_number }}
last »
{% endif %}
</span>
</div>
</div>
{% endfor %}
It's not a Django problem that your view is most certainly handle one pagination parameter page which will direct to courses, so if you changed the get parameter for each of your model pagination it will work.
ex:
## views.py
page = request.GET.get('page')
## yourtemplate.html
yourtext
localhost:8000/MYVIEW/?page=2 # Goes to courses
## views.py
page = request.GET.get('faculties')
## yourtemplate.html
yourtext
##
localhost:8000/MYVIEW/?faculties=2 # Goes to faculties
etc..
edited:
{% for faculty in faculties %} change it to {% for faculty in faculties2 %} and remove faculties from your context, cuz you are repeating yourself as faculties2 and faculties hold the same queryset .
paginator = Paginator(course_list, 1)
paginator = Paginator(faculty_list, 1)
Changing your second instance name to paginator_two would solve your problem

Django Pagination "Page not found"

I'm currently having issues with my Django pagination. I have a query set of 9 objects and am paginating one object to a page. So I should have 9 pages total.
Paginator is showing that I have 9 pages, but when I click the "next" page button my (Always on the last/last two pages) my url turns to: http://127.0.0.1:8000/forum/1?page=7
Then I get a 404 page not found error along with "Invalid page (7): That page contains no results"
Here is my class code:
class DirectoryClass(generic.ListView):
template_name = "forum_directory.html"
model = Directory
paginate_by = 1
def get_context_data(self, **kwargs):
context = super(DirectoryClass, self).get_context_data(**kwargs)
directory = Directory.objects.filter(pk=self.kwargs['directory_id'])
context['page'] = 'forum'
context['name'] = directory.first().name
context['user'] = self.request.user
topic_set = directory.first().topic_set.all().order_by('-last_post')
print(topic_set.all())
paginator = Paginator(topic_set, self.paginate_by)
page = self.request.GET.get('page')
try:
topic_set = paginator.page(page)
except PageNotAnInteger:
topic_set = paginator.page(1)
except EmptyPage:
topic_set = paginator.page(paginator.num_pages)
context['topics'] = topic_set
context['page'] = page
return context
Here is the HTML used to change the page:
<div style="width: 1240px; margin: auto; text-align: right;">
{% if topics.has_other_pages %}
<ul class="pagination forum-pagination">
{% if topics.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in topics.paginator.page_range %}
{% if topics.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if topics.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
</div>
Here is the url
path(r'/<int:directory_id>',views.DirectoryClass.as_view(), name='forum_directory'),
What am I doing wrong here?
def get_context_data(self, **kwargs):
context = super(DirectoryClass, self).get_context_data(**kwargs)
context['page'] = 'forum'
context['user'] = self.request.user
return context
def get_queryset(self):
directory = Directory.objects.filter(pk=self.kwargs['directory_id'])
topics = directory.first().topic_set.all().order_by('-last_post')
return topics
use this only in your ListView, as Django provides pagination to all the Class Based View

python pagination and search query on html

I am new to django.
I have a django template that renders mongodb values and accordingly paints the html. I am using pagination and search to display records.
Problem: How should I query search input in my html to display all the records which is currently masked with pagination in django.
This is how it looks with pagination.
And this is how I want to display with pagination
This is my code:
def index(request):
values = data.find()
paginator = Paginator(values, 12)
page = request.GET.get('page')
try:
listItem = paginator.page(page)
except PageNotAnInteger:
listItem = paginator.page(1)
except EmptyPage:
listItem = paginator.page(paginator.num_pages)
return render(request, 'product.html', {"values":values, "listItem":listItem})
This is my html
{% block content %}
<ul class="row catalog-list">
{% for value in values %}
<li class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div>
<img src={{value.image_url_medium}}>
</div>
<div>
<h4 class="ellipsis-text catalog-item-name" tooltip={{value.name}}>{{value.name}}</h4>
<h5>Product Id: {{value.id_product}}</h5>
<h5>Category: {{value.catagory}}</h5>
<h5>Best Price: {{value.best_price}}</h5>
<h5>Best Price Vendor: {{value.best_price_vendor}}</h5>
<h5 class="ellipsis-text">Link:
<a href={{value.best_price_vendor_url}}>{{value.best_price_vendor_url}}</a>
</h5>
</div>
</li>
{% endfor %}
</ul>
<ul class="pagination">
<li class="step-links">
{% if listItem.has_previous %}
Previous
{% endif %}
<span class="current">
Page {{ listItem.number }} of {{ listItem.paginator.num_pages }}.
</span>
{% if listItem.has_next %}
Next
{% endif %}
</li>
</ul>
{% endblock %}
and jQuery:
$itemList = $('.catalog-list li');
console.log($itemList.length); //Prints 12
$("#filter").keyup(function(){
var filter = $(this).val();
if(filter==null){
$itemList.hide();
return;
}
var regex = new RegExp(filter, "i");
$itemList.each(function(){
if ($(this).find('h4').text().search(regex) < 0 && $(this).find('h5').text().search(regex) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
I was new to django that time.
Its actually pretty simple here.
views.py
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from pymongo import MongoClient
register = template.Library()
port = 27017
client = MongoClient(port=port)
def people(request):
values = client.sdkUserIdDB.userIdColl.find() #getting all values from a database.
paginator = Paginator(values, 12) #putting all the values as a 12 slice so each page as 12 items from database.
page = request.GET.get('page')
try:
listItem = paginator.page(page)
except PageNotAnInteger:
listItem = paginator.page(1)
except EmptyPage:
listItem = paginator.page(paginator.num_pages)
return render(request, 'page.html', {"listItem":listItem})
templates > page.html
<ul class="pagination">
<li class="step-links">
{% if listItem.has_previous %}
Previous
{% endif %}
<span class="current">Page {{ listItem.number }} of {{ listItem.paginator.num_pages }}.</span>
{% if listItem.has_next %}
Next
{% endif %}
</li>
</ul>

Categories

Resources