Paginator in django - django

Hi i looking at https://docs.djangoproject.com/en/2.1/topics/pagination/ documentation about django paginator
in the code it use
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render
def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
page = request.GET.get('page')
contacts = paginator.get_page(page)
return render(request, 'list.html', {'contacts': contacts})
And in template it use
<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if contacts.has_next %}
next
last »
{% endif %}
</span>
</div>
I understand all the codes except in first time render what will be value of page
In the line
page = request.GET.get('page')
i know django run
?page=Value
and pass value to page argumant but what was the value of page in first render

page will be None in that case and get_page will then return the first page.

Related

Is there any multiple pagination on the same page but in different tabs in django?

I have tabs in a Django template displaying the tour packages category. For a single page, I am able to implement pagination in Django, but I do not know how to make pagination based on tabs. Any suggestions on how to filter in Django views based on tabbed heading?
In a single page, I am using it like so:
models.py:
def blog(request):
blog_list = Blog.objects.filter(blog_verification=True)
paginator = Paginator(blog_list, 6) # Show 6 blogs per page.
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {'blogs': page_obj, 'packages': Packages.objects.all()}
return render(request, 'user/blog.html', context)
Templates:
<div class="pagination">
<span class="step-links">
{% if blogs.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ blogs.number }} of {{ blogs.paginator.num_pages }}.
</span>
{% if blogs.has_next %}
next
last »
{% endif %}
</span>
</div>

How to load data from variable into django paginator?

I need to create paginator based on data from variable. Variable is just python lists.
views.py:
def test(request):
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="pc", # your username
passwd="3346378", # your password
db="mc")
cur = db.cursor()
cur.execute('''SELECT * FROM hello left join hell on hello.Id=hell.Id ''')
row = cur.fetchall()
So, row is lot's of lists. And how can I load it into paginator?
Or one way is to create table and model for data and work with it using Django ORM or something?
Django has built in Paginator class you can check this out: paginator objects and a few examples here: Pagination examples, to use in a view : Using Pagination in view
if it is referencing muliple objects then write plural variable names: so it is rows instead of row
So in your case in the view:
paginator = Paginator(rows, 25) # Show 25 rows per page
page = request.GET.get('page')
rows = paginator.get_page(page)
return render(request, 'example.html', {'rows': rows})
And in HTML:
{% for r in rows %}
{{ r }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if rows.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ rows.number }} of {{ rows.paginator.num_pages }}.
</span>
{% if rows.has_next %}
next
last »
{% endif %}
</span>
</div>

I don't know why my page isn't paginating i don't get any errors pls help :)

The page is supposed to get paginated but it doesn't I don't know what I did wrong. If anyone can help me figure it out i will appreciate
This is for a comment section on a site and I don't really know how to fix it. I've been looking the problem up on the web with no results then came here
from django.core.paginator import Paginator
def Home_view(request):
posts = Post.objects.order_by("-date_posted")
all_experiences = Experience.objects.order_by("-id")
all_educations = Education.objects.order_by("-id")
all_skills = Skill.objects.all()
paginator = Paginator(posts, 1)
page = request.GET.get('page')
post_list = paginator.get_page(page)
context = {
'posts': posts,
'all_experiences': all_experiences,
'all_educations': all_educations,
'all_skills': all_skills,
}
return render(request, 'resume.html', context)
Html Page supposed to get paginated
{% if is_paginated %}
<div class="pagination">
<span class="step-links">
{% if post_list.has_previous %}
« first
<a href="?page={{ post_list.previous_page_number }}">previous
</a>
{% endif %}
<span class="current">
Page{{post_list.number}}of{{post_list.paginator.num_pages}}.
</span>
{% if post_list.has_next %}
next
<a href="?page={{ post_list.paginator.num_pages }}">last»
</a>
{% endif %}
</span>
</div>
{% endif %}
{% else %}
the page is supposed to show 5 posts at once but doesn't and doesn't throw out any errors it just doesn't work
It looks like you aren't adding post_list to your context so {% if post_list.has_previous %} does nothing. You are passing posts which is the unpaginated list of posts. You will also need to add is_paginated to the context. Try updating context to something like:
context = {
'all_experiences': all_experiences,
'all_educations': all_educations,
'all_skills': all_skills,
'post_list': post_list,
'is_paginated': True,
}

Pagination Elasticsearch in Django

I am a newbie in Elasticsearch, I just try to create a search engine using it in Django. Overall, the engine shows good results. Unfortunately, it loads a large number of the results. Then, I try to paginate it by regular pagination in Django, after that, the page load error object of type 'Search' has no len().
These are my codes:
view.py
from django.shortcuts import render, redirect
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
from django.core.paginator import Paginator
def search_es(request):
return render(request,'search/search.html')
def results(request):
s = Search(using=Elasticsearch())
keyword = request.GET.get('q') # keyword that want to be found
print(keyword)
if keyword:
# posts = s.query('match_phrase_prefix',head_title=keyword)
# if posts.count() == 0:
posts = s.query(
"multi_match",
query=keyword,
fields=['head_title^5', 'description^5', 'description.ngram'],
# type="phrase_prefix",
)
posts = posts[0: 100]
else:
posts = ''
page = request.GET.get('page', 1)
paginator = Paginator(posts, 10)
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
context = {
'page_title': keyword,
'posts': users,
'count': posts.count(),
'keyword': keyword,
}
return render(request,'search/results.html',context)
results.html
{% if posts.has_other_pages %}
<ul class="pagination">
{% if posts.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in posts.paginator.page_range %}
{% if posts.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if posts.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
Hope for every possible solution.
Thank you very much.
ElasticSearch provided two parameters you can use for pagination: from and size. You can refer to https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#pagination
for example:
posts = s[0:20].query(
"multi_match",
query=keyword,
fields=['head_title^5', 'description^5', 'description.ngram'],
# type="phrase_prefix",
)
to get first page, and page size is 20.

How to get paginated answer's response?

I am using django as backend for website
There will be 10 qwestions
i want to paginate them (one qwestion's ans is given then go to next so on ) and atlast give marks calculated based on all the answers given
Any hints on how to do it?
I found somthing similar but dont know how to implement in django
What's the most efficient way to calculate a running total/balance when using pagination (PHP, MySQL)
Have you looked to Django Pagination docs?
https://docs.djangoproject.com/en/2.1/topics/pagination/
From that page
Your view:
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render
def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # 25 is the items per page, in Your case would be 1
page = request.GET.get('page')
contacts = paginator.get_page(page)
return render(request, 'list.html', {'contacts': contacts})
Your template:
{% for contact in contacts %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if contacts.has_next %}
next
last »
{% endif %}
</span>
</div>
To go to a page you should add to the url ?page=1 where 1 is the page number.