1.增刪改查前端
addpython
obj = Obj(atr=atr..)ajax
obj.save()json
update:app
__dict__ 遍歷異步
del:工具
根據id列表刪除3d
query:blog
首次查詢:it
直接跳轉至頁面,前端頁面自動調用ajax異步方法查詢
根據條件查詢:
使用分頁類和將參數字典化傳入
工具:
將request.POST or request.GET 的 keyValue turn into dictionary, 配合method (**kwargs)
分頁工具:
# 分頁查詢工具類 class Pagination(object): def __init__(self, totalCount, currentPage, perPageItemNum, maxPageNum): # 數據總個數 self.total_count = totalCount # 當前頁 try: v = int(currentPage) if v <= 0: v = 1 self.current_page = v except Exception as e: self.current_page = 1 # 每頁顯示的行數 self.per_page_item_num = perPageItemNum # 最多顯示頁面 self.max_page_num = maxPageNum def start(self): return (self.current_page-1) * self.per_page_item_num def end(self): return self.current_page * self.per_page_item_num @property def num_pages(self): """ 總頁數 """ a, b = divmod(self.total_count, self.per_page_item_num) if b == 0: return a return a+1 @property def pager_num_range(self): # self.current_page 當前頁 # self.per_pager_num 每頁顯示條數 # self.num_pages 總頁數 # self.max_page_num page導航欄總頁數 if self.num_pages < self.max_page_num: return range(1, self.num_pages+1) # 總頁數特別多 5 part = int(self.max_page_num/2) if self.current_page <= part: return range(1, self.max_page_num+1) if (self.current_page + part) > self.num_pages: return range(self.num_pages-self.max_page_num+1, self.num_pages+1) return range(self.current_page-part, self.current_page+part+1) def to_dict(self): return json.loads(json.dumps({'current_page': self.current_page, 'num_pages': self.num_pages, 'max_page_num': self.max_page_num, 'per_pager_num': self.per_page_item_num, 'pager_num_range': self.pager_num_range,})) def page_str(self): """ 在後臺封裝前臺分頁導航欄,適用於單頁面參數 前端頁面引用方法: <ul class="pagination pagination-sm" style="margin: 0 0"> {{ page_obj.page_str | safe }} <ul> :return: 分頁導航欄 """ page_list = [] first = "<li><a href='?p=1'>首頁</a></li>" page_list.append(first) if self.current_page == 1: prev = "<li><a href='#'>上一頁</a></li>" else: prev = "<li><a href='?p=%s'>上一頁</a></li>" % (self.current_page-1,) page_list.append(prev) for i in self.pager_num_range(): if i == self.current_page: temp = "<li class='active'><a href='?p=%s'>%s</a></li>" % (i, i) else: temp = "<li><a href='?p=%s'>%s</a></li>" % (i, i) page_list.append(temp) if self.current_page == self.num_pages: nex = "<li><a href='#'>下一頁</a></li>" else: nex = "<li><a href='?p=%s'>下一頁</a></li>" % (self.current_page + 1,) page_list.append(nex) last = "<li><a href='?p=%s'>尾頁</a></li>" % (self.num_pages,) page_list.append(last) return ''.join(page_list)