日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

django11:自动序列化/批量插入数据/分页器

發(fā)布時(shí)間:2023/12/4 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django11:自动序列化/批量插入数据/分页器 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自動(dòng)序列化

借助serializers幫你自動(dòng)完成序列化

from app01 import models from django.core import serializers def ab_se(request):user_queryset = models.Userinfo.objects.all()#原始方法user_list = []for user_obj in user_queryset:user_list.append({'username':user_obj.username,'password':user_obj.password,'gender':user_obj.get_gender_display(),})res = json.dumps(user_list)#方法****2res = serializers.serialize('json',user_queryset) # 序列化return HttpResponse(res)

?

批量插入數(shù)據(jù)

models.Book.objects.bulk_create(book_list)
?

分頁(yè)器

#原始分頁(yè) 切片 book_queryset=models.Book.objects.all()[0:10]

自定義

補(bǔ)充:

封裝代碼的思路

1.先用最粗糙的代碼實(shí)現(xiàn)功能

2.在功能基礎(chǔ)上,再考慮優(yōu)化

a.先函數(shù)

b.在對(duì)象

并不一定要面向?qū)ο?/strong>

?

用到非django內(nèi)置的第三方功能或組件代碼時(shí),

一般創(chuàng)建utils文件夾,一般在project根目錄,在文件夾內(nèi)對(duì)模塊進(jìn)行功能性劃分

utils可以在每個(gè)應(yīng)用創(chuàng)建,具體看實(shí)際情況。

class

class Pagination(object):def __init__(self,current_page,all_count,per_page_num=2,pager_count=11):"""封裝分頁(yè)相關(guān)數(shù)據(jù):param current_page: 當(dāng)前頁(yè):param all_count: 數(shù)據(jù)庫(kù)中的數(shù)據(jù)總條數(shù):param per_page_num: 每頁(yè)顯示的數(shù)據(jù)條數(shù):param pager_count: 最多顯示的頁(yè)碼個(gè)數(shù)用法:queryset = model.objects.all()page_obj = Pagination(current_page,all_count)page_data = queryset[page_obj.start:page_obj.end]獲取數(shù)據(jù)用page_data而不再使用原始的queryset獲取前端分頁(yè)樣式用page_obj.page_html"""try:current_page = int(current_page)except Exception as e:current_page = 1if current_page <1:current_page = 1self.current_page = current_pageself.all_count = all_countself.per_page_num = per_page_num# 總頁(yè)碼all_pager, tmp = divmod(all_count, per_page_num)if tmp:all_pager += 1self.all_pager = all_pagerself.pager_count = pager_countself.pager_count_half = int((pager_count - 1) / 2)@propertydef start(self):return (self.current_page - 1) * self.per_page_num@propertydef end(self):return self.current_page * self.per_page_numdef page_html(self):# 如果總頁(yè)碼 < 11個(gè):if self.all_pager <= self.pager_count:pager_start = 1pager_end = self.all_pager + 1# 總頁(yè)碼 > 11else:# 當(dāng)前頁(yè)如果<=頁(yè)面上最多顯示11/2個(gè)頁(yè)碼if self.current_page <= self.pager_count_half:pager_start = 1pager_end = self.pager_count + 1# 當(dāng)前頁(yè)大于5else:# 頁(yè)碼翻到最后if (self.current_page + self.pager_count_half) > self.all_pager:pager_end = self.all_pager + 1pager_start = self.all_pager - self.pager_count + 1else:pager_start = self.current_page - self.pager_count_halfpager_end = self.current_page + self.pager_count_half + 1page_html_list = []# 添加前面的nav和ul標(biāo)簽page_html_list.append('''<nav aria-label='Page navigation>'<ul class='pagination'>''')first_page = '<li><a href="?page=%s">首頁(yè)</a></li>' % (1)page_html_list.append(first_page)if self.current_page <= 1:prev_page = '<li class="disabled"><a href="#">上一頁(yè)</a></li>'else:prev_page = '<li><a href="?page=%s">上一頁(yè)</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="#">下一頁(yè)</a></li>'else:next_page = '<li><a href="?page=%s">下一頁(yè)</a></li>' % (self.current_page + 1,)page_html_list.append(next_page)last_page = '<li><a href="?page=%s">尾頁(yè)</a></li>' % (self.all_pager,)page_html_list.append(last_page)# 尾部添加標(biāo)簽page_html_list.append('''</nav></ul>''')return ''.join(page_html_list)

后端

def get_book(request):book_list = models.Book.objects.all()current_page = request.GET.get("page",1)all_count = book_list.count()page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10)page_queryset = book_list[page_obj.start:page_obj.end]return render(request,'booklist.html',locals())

前端

<div class="container"><div class="row"><div class="col-md-8 col-md-offset-2">{% for book in page_queryset %}<p>{{ book.title }}</p>{% endfor %}{{ page_obj.page_html|safe }}</div></div> </div>

?

?

參考:

https://www.cnblogs.com/guyouyin123/p/12173020.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的django11:自动序列化/批量插入数据/分页器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。