1. 数据的展示
数据通过ORM查询出来 对象列表 QuerySet
1. 普通的字段
对象.字段名 ——》 数据库中的值
<td>{{ customer.phone }}</td>
2. choices
((1,'男'))
对象.字段名 ——》 数据库中的值 1
对象.get_字段名_display() ——》 数据库中的值对应的显示值 男
<td>{{ _source_display }}</td>
3. 外键
对象.外键 ——》 关联的对象 定义 __str__ __repr__
对象.外键.字段名
4. 自定义方法
多对多:
#显示自定义已报班级 在models.py中def show_class(self):return ' | '.join([str(i) for i in self.class_list.all()])
<td>{{ customer.show_class }}</td>
自定义显示HTML代码:
from django.utils.safestring import mark_safedef show_status(self):status_color = {'signed': 'green', 'unregistered': '#208c72', 'studying': 'yellow', 'paid_in_full': 'blue', }return mark_safe('<span style="background-color: {};color: white;padding: 2px">{}</span>'.format( (self.status), _status_display()))
前端应用
<td>{{ customer.show_status }}</td>
2. 分页
总的是思想 就是 拿到总页面数除分页数 得到做大页面数,对最大页面数 进行判断规定显示几个页面
具体算法 如最大页面数是11 除余2 得到5 如我当前页数是7 就用7-5得到开头 7+5等于结尾
2 12 =10 因为开头2也算就11
users = [{'name': 'alex{}'.format(i), 'pwd': '123'} for i in range(1, 302)]def fycustomer_list(request):# Customer_all=models.Customer.objects.all()'''第一页 0 20第2页 20 40n (n-1)*20 开始 20*n结尾>>> divmod(9,2)(4, 1)>>> divmod(9,2)[0]4>>> divmod(9,2)[1]1'''try:page_num = int(('page', '1'))if page_num <= 0:page_num = 1except Exception as e: # 输入字符串也等于一page_num = 1per_num = 10# 总数量all_count = len(users)# 总页码数 除分页数page_count, more = divmod(all_count, per_num)if more: # 如果有余数就+1页page_count += 1# 最大页码数 获取的页面除二减去 用于开头max_show = 11half_show = max_show // 2# #7-5# page_start=page_num-half_show# page_end=page_num+half_show #7+5# 总页码数 < 最大显示页码数if page_count < max_show:page_start = 1page_end = page_countelse:# 处理左边极值# 当前页小于等于一半if page_num <= half_show:page_start = 1page_end = max_show# 当前的数 +页码的一半 大于页码数elif page_num + half_show >= page_count:page_start = page_count - max_show + 1page_end = page_countelse:page_start = page_num - half_show # 7-5 11的一半page_end = page_num + half_show # 7 + 5 12 page_list = []if page_num == 1: # 第一页选的页码page_numpage_list.append('<li class="disabled"><a>上一页</a></li>')else:page_list.append('<li><a href="?page={}">上一页</a></li>'.format(page_num - 1, )) # 点击上一页-1# print('aaaaa', page_start)for i in range(page_start, page_end + 1):if i == page_num:page_list.append('<li class="active"><a href="?page={}">{}</a></li>'.format(i, i)) # 等于就激活当前页面 其他继续显示else:page_list.append('<li><a href="?page={}">{}</a></li>'.format(i, i))if page_num == page_count:page_list.append('<li class="disabled"><a>下一页</a></li>')else:page_list.append('<li><a href="?page={}">下一页</a></li>'.format(page_num + 1, )) # 点击下一页+1 page_html = ''.join(page_list)return render(request, 'user_list.html', {'users': users[(page_num - 1) * per_num:per_num * page_num],'page_html': page_html})
html
<table class="table table-hover table-bordered">{% for user in users %}<tr><td>{{ user.name }}</td><td>{{ user.pwd }}</td></tr>{% endfor %}</table><nav aria-label="Page navigation"><ul class="pagination">{{ page_html|safe }}</ul></nav>
封装成类
创建文件 utils 创建python文件pagination.py
class Pagination:#当前页面数 总页码数 显示页码数 最大分页数def __init__(self, page_num, all_count, per_num=10, max_show=11):# 获取页码try:self.page_num = int(page_num)if self.page_num <= 0:self.page_num = 1except Exception as e:self.page_num = 1# 每页显示的数据量self.per_num = per_num# 总数据量all_count = all_count# 总页码数self.page_count, more = divmod(all_count, per_num)if more:self.page_count += 1# 最大显示页码数self.max_show = max_showself.half_show = max_show // 2@propertydef page_html(self):# 总页码数 < 最大显示页码数if self.page_count < self.max_show:page_start = 1page_end = self.page_countelse:# 处理左边极值if self.page_num <= self.half_show:page_start = 1page_end = self.max_showelif self.page_num + self.half_show >= self.page_count:page_start = self.page_count - self.max_show + 1page_end = self.page_countelse:page_start = self.page_num - self.half_show # 2page_end = self.page_num + self.half_show # 7 + 5 12 page_list = []if self.page_num == 1:page_list.append('<li class="disabled"><a>上一页</a></li>')else:page_list.append('<li><a href="?page={}">上一页</a></li>'.format(self.page_num - 1, ))for i in range(page_start, page_end + 1):if i == self.page_num:page_list.append('<li class="active"><a href="?page={}">{}</a></li>'.format(i, i))else:page_list.append('<li><a href="?page={}">{}</a></li>'.format(i, i))if self.page_num == self.page_count:page_list.append('<li class="disabled"><a>下一页</a></li>')else:page_list.append('<li><a href="?page={}">下一页</a></li>'.format(self.page_num + 1, ))return ''.join(page_list)@propertydef start(self):"""切片的起始值:return:"""return (self.page_num - 1) * self.per_num@property#封装成属性def end(self):"""切片的终止值:return:"""return self.page_num * self.per_num
关于 用户登陆 需要 session 认证 可以写个中间件 进行全局变量
创建middlewares 添加 auth.py
from django.utils.deprecation import MiddlewareMixin from crm import models from django.shortcuts import redirect, reverse class AuthMiddleware(MiddlewareMixin):def process_request(self, request):if request.path_info in [reverse('login'), reverse('reg')]:#白名单returnif request.path_info.startswith('/crm/admin/'):returnpk = ('pk')user = models.UserProfile.objects.filter(pk=pk).first()# 没有登录 跳转至登录页面if not user:return redirect(reverse('login'))request.user_obj = user
转载于:.html
本文发布于:2024-02-01 07:04:48,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170674229034757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |