from django.shortcuts import render
from django.http import JsonResponse
from django.core.paginator import Paginator
from .models import Articles
def pagination_pro(request):
#model
my_model = Articles.objects.all()
#number of items on each page
number_of_item = 10
#Paginator
paginatorr = Paginator(my_model, number_of_item)
#query_set for first page
first_page = paginatorr.page(1).object_list
#range of page ex range(1, 3)
page_range = paginatorr.page_range
context = {
'paginatorr':paginatorr,
'first_page':first_page,
'page_range':page_range
}
#
if request.method == 'POST':
page_n = request.POST.get('page_n', None) #getting page number
results = list(paginatorr.page(page_n).object_list.values('id', 'title'))
return JsonResponse({"results":results})
return render(request, 'index.html',context)
class Articles(models.Model):
title = models.CharField(max_length=300)
def __str__(self):
return self.title
from django.urls import path
from core.views import *
urlpatterns = [
path('pagination/', pagination_pro, name='pagination_p'),
]
<html>
<head>
<title>How to make a pagination with django and jquery</title>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<!-- queryset for first page -->
<div id="posts">
{% for i in first_page %}
<h2>{{i.title}}</h2>
{% endfor %}
</div>
<!-- loop pagination -->
{% for i in page_range %}
<a style="margin-left: 5px; font-size: 20px;" href="{{i}}">{{i}}</a>
{% endfor %}
<script>
$('a').click(function(event){
event.preventDefault();
var page_n = $(this).attr('href');
// ajax
$.ajax({
type: "POST",
url: "{% url 'pagination_p' %}", // name of url
data : {
page_n : page_n, //page_number
csrfmiddlewaretoken: '{{ csrf_token }}',
},
success: function (resp) {
//loop
$('#posts').html('')
$.each(resp.results, function(i, val) {
//apending posts
$('#posts').append('<h2>' + val.title + '</h2>')
});
},
error: function () {}
}); //
});
</script>
</body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="author" content="Sahil Kumar" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Tabbed Style Login & Registration Form Using CSS3 & jQuery</title>
<link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet" />
<link rel="stylesheet" href="style.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="main">
<!-- Tab Buttons -->
<div id="tab-btn">
<a href="#" class="login-tab active">Sign In</a>
<a href="#" class="register-tab">Sign Up</a>
</div>
<!-- Login Form -->
<div class="login-box">
<h2>Get Started!</h2>
<form action="#" method="post" id="login-form">
<input type="text" name="username" placeholder="Username" class="inp" required autofocus /><br />
<input type="password" name="password" placeholder="Password" class="inp" required /><br />
<a href="#" id="forgot">Forgot Password?</a><br />
<input type="submit" name="submit" value="SIGN IN" class="sub-btn" />
</form>
</div>
<!-- Registration Form -->
<div class="register-box">
<h2>Register With Us!</h2>
<form action="#" method="post" id="reg-form">
<input type="text" name="uname" placeholder="Enter Username" class="inp" required autofocus /><br />
<input type="email" name="email" placeholder="Enter Email" class="inp" required /><br />
<input type="password" name="pass" placeholder="Enter Password" class="inp" required /><br />
<input type="password" name="repass" placeholder="Confirm Password" class="inp" required /><br />
<input type="submit" name="submit" value="SIGN UP" class="sub-btn" />
</form>
</div>
</div>
</body>
</html>
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: "Cabin";
}
body {
background-color: #009688;
display: flex;
min-height: 100vh;
justify-content: center;
align-items: center;
}
#main {
max-width: 95%;
width: 500px;
}
#tab-btn {
display: flex;
}
.login-tab,
.register-tab {
text-decoration: none;
display: block;
width: 100%;
background-color: #37474f;
text-align: center;
color: #fff;
font-size: 25px;
padding: 10px;
}
.active {
background-color: #eee;
color: #000;
}
.login-box,
.register-box {
width: 100%;
background-color: #eee;
padding: 20px 30px 30px 30px;
box-shadow: 2px 2px 5px #555;
}
.register-box {
display: none;
}
.inp {
width: 100%;
padding: 12px;
margin-bottom: 15px;
font-size: 20px;
border: 2px solid #888;
border-bottom: 5px solid #888;
background-color: #fff;
color: #666;
}
.inp:focus {
outline: none;
border: 2px solid #555;
border-bottom: 5px solid #555;
}
.sub-btn {
width: 100%;
padding: 12px;
border: none;
font-size: 20px;
font-weight: bold;
background-color: #f44336;
color: #fff;
cursor: pointer;
border-bottom: 5px solid #444;
}
.sub-btn:focus {
outline: none;
}
.sub-btn:active {
border: none;
margin-top: 5px;
}
#forgot {
font-size: 20px;
color: #444;
text-decoration: none;
display: block;
}
h2 {
text-align: center;
padding-bottom: 15px;
font-variant: small-caps;
color: #333;
font-size: 30px;
}
$(document).ready(function () {
$(".register-tab").click(function () {
$(".register-box").show();
$(".login-box").hide();
$(".register-tab").addClass("active");
$(".login-tab").removeClass("active");
});
$(".login-tab").click(function () {
$(".login-box").show();
$(".register-box").hide();
$(".login-tab").addClass("active");
$(".register-tab").removeClass("active");
});
});