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.
Related
I'm making a portfolio project where I'm using the Google Books API to do a books search, and the Django Paginator class to paginate the results. I've been able to get search results using a CBV FormView and a GET request, but I can't seem to figure out how to get pagination working for the API response.
The solution I can think of is to append &page=1 to the url of the first search, then pull that param on every GET request and use that to paginate. The problem is, I can't figure out how to append that param on the first search, and I don't know how I'd increment that param value when clicking the pagination buttons.
Here's what I've got now:
Form:
class SearchForm(forms.Form):
search = forms.CharField(label='Search:', max_length=150)
View:
class HomeView(FormView):
template_name = "home.html"
form_class = SearchForm
pageIndex = 0
def get(self, request, *args, **kwargs):
# get submitted results in view and display them on results page. This will be swapped out for an AJAX call eventually
if "search" in request.GET:
# getting search from URL params
search = request.GET["search"]
kwargs["search"] = search
context = super().get_context_data(**kwargs)
# Rest API request
response = requests.get(
f'https://www.googleapis.com/books/v1/volumes?q={search}&startIndex={self.pageIndex}&key={env("BOOKS_API_KEY")}'
)
response = response.json()
items = response.get("items")
# pagination...needs work
paginator = Paginator(items, 2)
page_obj = paginator.get_page(1)
context["results"] = page_obj
return self.render_to_response(context)
else:
return self.render_to_response(self.get_context_data())
Template:
{% extends "base.html" %}
{% block content %}
<form action="/">
{{ form }}
<input type="submit" value="Submit">
</form>
<h1>Books</h1>
<ul>
{% for result in results %}
<li>{{ result.volumeInfo.title }} : {{result.volumeInfo.authors.0}}</li>
{% empty %}
<li>Search to see results</li>
{% endfor %}
</ul>
{% if results %}
<div class="pagination">
<span class="step-links">
{% if results.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ results.number }} of {{ results.paginator.num_pages }}
</span>
{% if results.has_next %}
next
last »
{% endif %}
</span>
</div>
{% endif %}
{% endblock content %}
I also looked at Django REST Framework for this, but the Google Books API response doesn't contain any info on next page, previous page, etc. I've done this kind of pagination in React and it's not difficult, I'm just having trouble adjusting my mental model for how to do this to Django. If anyone could offer some advice on how to make this work, I'd be very grateful.
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>
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>
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,
}
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.