django----Sweetalert bulk_create批量插入数据 自定义分页器
一.Sweetalert使用AJAX操作
sweetalert下载地址 Sweetalert
$("#b55").click(function () {
swal({
title: "你确定要删除吗?",
text: "删除可就找不回来了哦!",
type: "warning",
showCancelButton: true, // 是否显示取消按钮
confirmButtonClass: "btn-danger", // 确认按钮的样式类
confirmButtonText: "删除", // 确认按钮文本
cancelButtonText: "取消", // 取消按钮文本
closeOnConfirm: false, // 点击确认按钮不关闭弹框
showLoaderOnConfirm: true // 显示正在删除的动画效果
},
function () {
var deleteId = 2;
$.ajax({
url: "/delete_book/",
type: "post",
data: {"id": deleteId},
success: function (data) {
if (data.code === 0) {
swal("删除成功!", "你可以准备跑路了!", "success");
} else {
swal("删除失败", "你可以再尝试一下!", "error")
}
}
})
});
})
页面刷新复习 location.reload
// 利用Dom操作
$(this).parent().parent().remove()
// function里的this 和 之前的this不一样 所以我们需要 之前就定义好this
$btn = $(this)
二.bulk_create
def index(request):
for i in range(1000):
models.Book.objects.create(title='%s'%i)
book_queryset = models.Book.objects.all()
return render('xxx.html',locals())
使用bulk_create 来批量插入数据
def index(request):
book_list = []
for i in range(10000):
book_list.append(models.Book(title='第%s本书'%i))
# 批量插入数据 建议orm建议你使用bulk_create
models.Book.objects.bulk_create(book_list)
book_queryset = models.Book.objects.all()
return render('xxx.html', locals())
两者差距很大
三.分页器
divmod
>>> divmod(100,10)
(10, 0)
>>> divmod(101,10)
(10, 1)
>>> divmod(99,10)
(9, 9)
分页器组件
通常 我们使用到外部的功能 都会放入 文件名为 utils
class Pagination(object):
def __init__(self,current_page,all_count,per_page_num=2,pager_count=11):
"""
封装分页相关数据
:param current_page: 当前页
:param all_count: 数据库中的数据总条数
:param per_page_num: 每页显示的数据条数
:param pager_count: 最多显示的页码个数
用法:
queryset = model.objects.all()
page_obj = Pagination(current_page,all_count)
page_data = queryset[page_obj.start:page_obj.end]
获取数据用page_data而不再使用原始的queryset
获取前端分页样式用page_obj.page_html
"""
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
if current_page <1:
current_page = 1
self.current_page = current_page
self.all_count = all_count
self.per_page_num = per_page_num
# 总页码
all_pager, tmp = divmod(all_count, per_page_num)
if tmp:
all_pager += 1
self.all_pager = all_pager
self.pager_count = pager_count
self.pager_count_half = int((pager_count - 1) / 2)
@property
def start(self):
return (self.current_page - 1) * self.per_page_num
@property
def end(self):
return self.current_page * self.per_page_num
def page_html(self):
# 如果总页码 < 11个:
if self.all_pager <= self.pager_count:
pager_start = 1
pager_end = self.all_pager + 1
# 总页码 > 11
else:
# 当前页如果<=页面上最多显示11/2个页码
if self.current_page <= self.pager_count_half:
pager_start = 1
pager_end = self.pager_count + 1
# 当前页大于5
else:
# 页码翻到最后
if (self.current_page + self.pager_count_half) > self.all_pager:
pager_end = self.all_pager + 1
pager_start = self.all_pager - self.pager_count + 1
else:
pager_start = self.current_page - self.pager_count_half
pager_end = self.current_page + self.pager_count_half + 1
page_html_list = []
# 添加前面的nav和ul标签
page_html_list.append('''
<nav aria-label='Page navigation>'
<ul class='pagination'>
''')
first_page = '<li><a href="?page=%s">首页</a></li>' % (1)
page_html_list.append(first_page)
if self.current_page <= 1:
prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
else:
prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,)
page_html_list.append(prev_page)
for i in range(pager_start, pager_end):
if i == self.current_page:
temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,)
else:
temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,)
page_html_list.append(temp)
if self.current_page >= self.all_pager:
next_page = '<li class="disabled"><a href="#">下一页</a></li>'
else:
next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,)
page_html_list.append(next_page)
last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,)
page_html_list.append(last_page)
# 尾部添加标签
page_html_list.append('''
</nav>
</ul>
''')
return ''.join(page_html_list)
自定义分页器的使用
# 一页展示多少条
per_page_num = 10
# 用户想看的页码 但是拿到的字符串我们强转 默认为1
current_page_str = request.GET.get('page',1)
current_page_num = int(current_page_str)
"""
per_page_num = 10
current_page start_page end_page
1 0 10
2 10 20
3 20 30
"""
# 开始条数
start_page = (current_page_num - 1) * per_page_num
end_page = current_page_num * per_page_num
# 一共分多少页展示给用户看
book_count_num = models.AuthDetail.objects.count()
all_count, more = divmod(book_count_num, per_page_num)
if more:
all_count += 1
# 可以切分渲染给前端页面了
book_queryset = models.AuthDetail.objects.all()
xxx = current_page_num
# 判断他是否是前6页
if current_page_num < 6:
xxx = 6
if current_page_num > all_count - 5:
xxx = all_count - 5
# for循环自造数据给前端
html = ''
for i in range(xxx-5, xxx+6):
# 判断如果循环的时候 current_page_num == i 说明当前页
if current_page_num == i:
html += '<li class="active"><a href="?page=%s">%s</a></li>'%(i,i)
else:
html += '<li><a href="?page=%s">%s</a></li>' % (i, i)
# 安全返回
from django.utils.safestring import mark_safe
html = mark_safe(html)
book_queryset = book_queryset[start_page:end_page]
return render(request, 'test.html', locals())
current_page = request.GET.get('page',1)
all_count = book_queryset.count()
page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10,pager_count=5)
# 需要将原来的 book_queryset 替换成 page_queryset
page_queryset = book_queryset[page_obj.start:page_obj.end]
# book_queryset = book_queryset[start_page:end_page]
return render(request,'index.html',locals()) # 第二种
最后 渲染分页器 <p>{{ page_ojb.page_html|safe }}</p>
django----Sweetalert bulk_create批量插入数据 自定义分页器的更多相关文章
- [Django高级之批量插入数据、分页器组件]
[Django高级之批量插入数据.分页器组件] 批量插入数据 模板层models.py from django.db import models class Books(models.Model): ...
- django与ajax:ajax结合sweetalter ,批量插入数据 ;分页器组件
目录 一.ajax结合sweetalter 二.bulk_create批量插入数据 三.简易版分页器推导 1. 推导步骤 四.自定义分页器的使用 1. 自定义分页器模板 2. 使用方法 (1)后端代码 ...
- Django批量插入数据和分页器
目录 一.ajax结合sweetalert实现删除按钮动态效果 二.bulk_create批量插入数据 1. 一条一条插入 2. 批量插入 三.自定义分页器 一.ajax结合sweetalert实现删 ...
- django ajax 及批量插入数据 分页器
``` Ajax 前端朝后端发送请求都有哪些方式 a标签href GET请求 浏览器输入url GET请求 form表单 GET/POST请求 Ajax GET/POST请求 前端朝后端发送数据的编码 ...
- Django orm 实现批量插入数据
Django ORM 中的批量操作 在Hibenate中,通过批量提交SQL操作,部分地实现了数据库的批量操作.但在Django的ORM中的批量操作却要完美得多,真是一个惊喜. 数据模型定义 首先,定 ...
- bulk_create 批量插入数据
def booklist(request): # 动态插入100条数据 for i in range(100): models.Book2.objects.create(name='第%s本书'%i) ...
- Django向数据库批量插入数据
# 如何向数据库一次性插入多条数据 # 方法一:效率极低,不推荐使用 for i in range(1000): models.Book.objects.create(title=f'第{i}本书') ...
- Django-choices字段值对应关系(性别)-MTV与MVC科普-Ajax发json格式与文件格式数据-contentType格式-Ajax搭配sweetalert实现删除确认弹窗-自定义分页器-批量插入-07
目录 models 字段补充 choices 参数/字段(用的很多) MTV与MVC模型 科普 Ajax 发送 GET.POST 请求的几种常见方式 用 Ajax 做一个小案例 准备工作 动手用 Aj ...
- django之ajax结合sweetalert使用,分页器和bulk_create批量插入 07
目录 sweetalert插件 bulk_create 批量插入数据 分页器 简易版本的分页器的推导 自定义分页器的使用(组件) sweetalert插件 有这么一个需求: 当用户进行一个删除数据 ...
随机推荐
- 什么是TCP, UDP, HTTP, HTTPS协议?
TCP 传输控制协议是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC793定义. TCP主要特点: 1. 面向连接: (1)应用程序在使用TCP协议之前,必须先建立TCP连接. ...
- UCACO刷题
UCACO刷题 SUBMIT: /* ID: your_id_here LANG: C++ TASK: test */ 文件:freopen(“file.in", "r" ...
- 十、GAP
1.1 背景 GAP(Generic Access Profile)位于主机协议栈的最顶层,用来定义BLE设备在待机或者连接状态中的行为,该Profile保证不同的Bluetooth产品可以互 ...
- k8s 随记
1.kubelet参数解析:https://blog.csdn.net/qq_34857250/article/details/84995381 2.如何在github中查找k8s代码关键字? 现在我 ...
- 2019-9-11:渗透测试,基础学习,ubuntu搭建LAMP
一,apache web服务器安装 1,sudo apt-get install apache2 2,systemctl status apache2,检查apache2是否开启 #开启.关闭和重启a ...
- es6 map的用法
let arr =[ {title:'aaaa',read:100,hot:true}, {title:'bbbb',read:50,hot:false}, {title:'ccc',read:100 ...
- BeanUtils.copyProperties()怎样去掉字段首尾的空格
背景 下午三时许,笔者正戴着耳机听着歌开心的敲着bug,忽然听到办公室的吵架声,原来是ios开发和产品小姐姐吵起来了,为了一个车牌号的校验问题.起因是ios传的车牌号没有将字符串的首尾空格去掉,后端直 ...
- Java基础面试题及答案(三)
多线程 35. 并行和并发有什么区别? 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 并行是在不同实体上的多个事件,并发是在同一实体上的多个事件. 在一台处理器 ...
- 管道符和作业控制、shell变量、环境变量配置文件 使用介绍
第6周第1次课(4月23日) 课程内容: 8.6 管道符和作业控制 8.7/8.8 shell变量8.9 环境变量配置文件扩展bashrc和bash_profile的区别 http://ask.ape ...
- 网站出现bug,我深夜被叫醒处理,用一个触发器解决了问题
凌晨两点,我正在睡梦之中,此时电话忽然想起,在漆黑的深夜中显得格外刺耳. 这个时间点电话响了肯定没好事,因为我的手机在夜间模式下,除非被同一个电话号码打三次,否则是静音,因此电话那边的人肯定有急事找我 ...