djagngo crm 项目 展示页面 和分页
1,在引入的时候使用{% load static%} {%static '路径'%}
使之变为动态的
2,使用母版和继承:
先建立母版,在不同的地方使用block ,在自己使用的html使用extends
3,显示字段注意事项:
在空的时候使用default:'暂无'
在时间使用的时候注意:
在setting配置:
USE_L10N = False
DATE_FORMAT = 'Y-m-d'
DATETIME_FORMAT = 'Y-m-d H:i:s'
在前端获取choice的时候使用:get_字段名_display
在有ForeignKey的时候需要在点出来
在设置色彩等自定义的属性的时候:
在后端:
from django.utils.html import format.html或者mark_safe
在表的字段中自定义:
'signed': "yellowgreen",
'unregistered': "grey",
'studying': 'red',
'paid_in_full': "#821e1e",
}
return format_html('<span style="color: white;padding: 3px">{}</span>'.format(color_dict[self.status],
self.get_status_display()))
设置多对多的时候:
定义函数:
在后端全部取出:
def show_class(self):
return "|".join([str(i) for i in self.class_list.all()])
并且需要自己在另一个字段添加str方法
4,设置logo:
<link rel="icon" href="{% static '/imgs/luffy-logo.png' %}">
注意放在引入处
分页:
函数:
版本1:
def user_list(request):
# try:
# current_page = int(request.GET.get('page'))
# if current_page <= 0: #在url后边等于0或者小于0时,直接跳到1页
# raise Exception()
# except Exception as e:
# current_page = 1
#
# max_show = 11 #最多显示的页码
# half_show = max_show // 2 #取到中间的的页码
# all_count = len(users) #总的数据量
# per_num = 10 #每一页显示的数据量
# total_page,more = divmod(all_count,per_num) #总页码数
# if more: #如果有余数则页数加一
# total_page += 1
#
# '''
# 1 1 10 0 10
# 2 11 20 10 20
#
# '''
# start = (current_page - 1) * per_num #索引开始
# end = current_page * per_num #索引结束
#
# if total_page < max_show: #如果不足定义的页数
# page_start = 1
# page_end = total_page
# else:
# if current_page <= half_show: #如果点击小于五的页数
# page_start = 1
# page_end = max_show
# elif current_page+half_show > total_page:#如果显示的页码超过总页数
# page_start = total_page - max_show + 1
# page_end = total_page
# else:
# page_start = current_page - half_show
# page_end = current_page + half_show
#
# return render(request,'user_list.html',{'data':users[start:end],
# 'total_page':range(page_start,page_end+1)
# })
# def user_list(request):
# try:
# current_page = int(request.GET.get('page'))
# if current_page <= 0: # 在url后边等于0或者小于0时,直接跳到1页
# raise Exception()
# except Exception as e:
# current_page = 1
#
# max_show = 11 # 最多显示的页码
# half_show = max_show // 2 # 取到中间的的页码
# all_count = len(users) # 总的数据量
# per_num = 10 # 每一页显示的数据量
# total_page, more = divmod(all_count, per_num) # 总页码数
# if more: # 如果有余数则页数加一
# total_page += 1
#
# '''
# 1 1 10 0 10
# 2 11 20 10 20
#
# '''
# start = (current_page - 1) * per_num # 索引开始
# end = current_page * per_num # 索引结束
#
# if total_page < max_show: # 如果不足定义的页数
# page_start = 1
# page_end = total_page
# else:
# if current_page <= half_show: # 如果点击小于五的页数
# page_start = 1
# page_end = max_show
# elif current_page + half_show > total_page: # 如果显示的页码超过总页数
# page_start = total_page - max_show + 1
# page_end = total_page
# else:
# page_start = current_page - half_show
# page_end = current_page + half_show
# html_list = []
# if current_page <=1: #显示上一页的
# prev_li = '<li class="disabled"><a><上一页></a></li>'
# else:
# prev_li = '<li ><a href="/user_list/?page={0}"><上一页></a></li>'.format(current_page-1)
# html_list.append(prev_li)
# for i in range(page_start,page_end+1):
# if i == current_page:
# li_html = '<li class="active"><a href="/user_list/?page={0}">{0}</a></li>'.format(i)
# else:
# li_html = '<li><a href="/user_list/?page={0}">{0}</a></li>'.format(i)
# html_list.append(li_html)
# if current_page >= total_page:
# next_li = '<li class="disabled"><a>< 下一页 ></a></li>'
# else:
# next_li = '<li><a href="/user_list/?page={0}">< 下一页 ></a></li>'.format(current_page + 1)
# html_list.append(next_li)
# html_str = "".join(html_list)
# html_str = format_html(html_str)
# return render(request, 'user_list.html', {'data': users[start:end],
# 'html_str': html_str
#
面向对象:
from django.utils.html import mark_safe
class Pagnation(object):
def __init__(self, request, all_count, base_url, per_num=10, max_show=11):
try:
current_page = int(request.GET.get('page'))
if current_page <= 0: # 在url后边等于0或者小于0时,直接跳到1页
raise Exception()
except Exception as e:
current_page = 1
self.base_url = base_url
self.current_page = current_page
self.max_show = max_show # 最多显示的页码
self.half_show = max_show // 2 # 取到中间的的页码
self.all_count = all_count # 总的数据量
self.per_num = per_num # 每一页显示的数据量
self.total_page, more = divmod(self.all_count, per_num) # 总页码数
if more: # 如果有余数则页数加一
self.total_page += 1
'''
1 1 10 0 10
2 11 20 10 20
'''
@property
def start(self):
return (self.current_page - 1) * self.per_num # 索引开始
@property
def end(self):
return self.current_page * self.per_num # 索引结束
@property
def html_str(self):
if self.total_page < self.max_show: # 如果不足定义的页数
page_start = 1
page_end = self.total_page
else:
if self.current_page <= self.half_show: # 如果点击小于五的页数
page_start = 1
page_end = self.max_show
elif self.current_page + self.half_show > self.total_page: # 如果显示的页码超过总页数
page_start = self.total_page - self.max_show + 1
page_end = self.total_page
else:
page_start = self.current_page - self.half_show
page_end = self.current_page + self.half_show
html_list = []
if self.current_page <= 1: # 显示上一页的
prev_li = '<li class="disabled"><a><上一页></a></li>'
else:
prev_li = '<li ><a href="/user_list/?page={0}"><上一页></a></li>'.format(self.current_page - 1)
html_list.append(prev_li)
for i in range(page_start, page_end + 1):
if i == self.current_page:
li_html = '<li class="active"><a href="/user_list/?page={0}">{0}</a></li>'.format(i)
else:
li_html = '<li><a href="/user_list/?page={0}">{0}</a></li>'.format(i)
html_list.append(li_html)
if self.current_page >= self.total_page:
next_li = '<li class="disabled"><a>< 下一页 ></a></li>'
else:
next_li = '<li><a href="/user_list/?page={0}">< 下一页 ></a></li>'.format(self.current_page + 1)
html_list.append(next_li)
return mark_safe("".join(html_list))
"""
使用方法:
from utils.pagination import Pagination
def user_list(request):
page_obj = Pagination(request, len(users),request.path_info, 20,5)
return render(request,
'user_list.html',
{
"data": users[page_obj.start:page_obj.end],
"html_str": page_obj.html_str,
})
"""
<div class="text-center">
<nav aria-label="Page navigation ">
<ul class="pagination">
{# {% for page in total_page %}#}
{# <li><a href="/user_list/?page={{ page }}">{{ page }}</a></li>#}
{# {% endfor %}#}
{{ html_str }}
</ul>
</nav>
</div>