首页 > 代码库 > django 编程小结
django 编程小结
1.增删改查
add
obj = Obj(atr=atr..)
obj.save()
update:
__dict__ 遍历
del:
根据id列表删除
query:
首次查询:
直接跳转至页面,前端页面自动调用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=http://www.mamicode.com/‘?p=1‘>首页" page_list.append(first) if self.current_page == 1: prev = "<li><a href=http://www.mamicode.com/‘#‘>上一页" else: prev = "<li><a href=http://www.mamicode.com/‘?p=%s‘>上一页" % (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=http://www.mamicode.com/‘?p=%s‘>%s" % (i, i) else: temp = "<li><a href=http://www.mamicode.com/‘?p=%s‘>%s" % (i, i) page_list.append(temp) if self.current_page == self.num_pages: nex = "<li><a href=http://www.mamicode.com/‘#‘>下一页" else: nex = "<li><a href=http://www.mamicode.com/‘?p=%s‘>下一页" % (self.current_page + 1,) page_list.append(nex) last = "<li><a href=http://www.mamicode.com/‘?p=%s‘>尾页" % (self.num_pages,) page_list.append(last) return ‘‘.join(page_list)
django 编程小结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。