day58
作者:互联网
bulk_create批量插入数据
book_list=[]
for i in rage(10000):
book_list.append(models.book(title='第%本书'%i))
models.Book.object.bulk_create(book_list)
sweetalert
def home(request):
if request.is_ajax():
back_dic = {'code':1000,'msg':''}
delete_id = request.POST.get('delete_id')
time.sleep(3)
models.User.objects.filter(pk=delete_id).delete()
back_dic['msg'] = '数据已经被我删掉了'
return JsonResponse(back_dic)
user_queryset = models.User.objects.all()
return render(request,'home.html',locals())
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h2 class="text-center">数据展示</h2>
<br>
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>序号</th>
<th>用户名</th>
<th>年龄</th>
<th>性别</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
{% for userObj in user_queryset %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ userObj.username }}</td>
<td>{{ userObj.age }}</td>
<td>{{ userObj.get_gender_display }}</td>
<td class="text-center">
<a href="#" class="btn btn-primary btn-sm">编辑</a>
<a href="#" class="btn btn-danger btn-sm cancel" userId = {{ userObj.pk }}>删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<script>
$('.cancel').click(function () {
var $btn = $(this);
swal({
title: "你确定要删吗?",
text: "你如果删了,你就准备好直接跑路吧!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "是的,老子就要删!",
cancelButtonText: "算了算了,不敢!",
closeOnConfirm: false,
closeOnCancel: false,
showLoaderOnConfirm: true
},
function (isConfirm) {
if (isConfirm) {
// 朝后端发送ajax请求
$.ajax({
url:'',
type:'post',
data:{'delete_id':$btn.attr('userId')},
success:function (data) {
if(data.code==1000){
swal("准备跑路把!", data.msg, "success");
// 通过DOM操作 来直接操作标签
$btn.parent().parent().remove()
}else{
swal("有Bug", "发什么了未知的错误!", "warning");
}
}
});
} else {
swal("怂逼", "数据都不敢删", "error");
}
});
})
</script>
自定义分页器
后端
book_queryset=models.book.objects.all()#你想要分页展示的数据 current_page=request.GET.get('page',1)#获取当前页 all_count=book_queryset.count()#看一共有多少条数据 page_queryset=book_queryset[page_obj.start:page_obj.end] return render (request.'index.html',locals())
前端
{% for book in page_queryset %} <p>{{book}}</p>` {% endfor %} {{page_obj.page_html|safe}}告诉这是安全的可以读取
标签:models,queryset,request,day58,book,page,delete 来源: https://www.cnblogs.com/shenblog/p/11974640.html