django 编程小结
Posted 瓦肯船长
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=\'?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)
以上是关于django 编程小结的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段