django - Templates模板嵌套语法
模板继承
1、继承母板:{% extends '母板html文件名称' %}
2、包含子模板:{% include '子母板html 文件名' %}
模板内容分块 {% block <分块名> %}{% endblock %}
对某一区域分块命名,继承模板时覆盖 “母板” 中的同名块儿 {% block title %}{% endblock %}
3、利用request 请求对象中内容,例如session {{ request.session.current_user }}
自定义python函数应用于模板
(方法一:simple_tag)
1、在app 目录下创建templatetags 目录(名称不可更改)
2、创建任意.py文件,如xxoo.py ,在文件内写自定义功能的函数。使用django.template.Library()创建register对象(对象名不可更改)
使用 @register.simple_tag 装饰自定义函数
示例xxoo.py
from django import template register = template.Library() @register.simple_tag def plus(a1, a2): return a1 + a2
对象名必须为register
3、模板中使用 {% load '书写自定义函数的文件名' %}
函数用法 {% 函数名 参数...%}
{% load xxoo %}
{% plus 1 3 %}
优点:参数个数没有限制,缺点:不能使用if语句
(方法二:filter)
1、在app 目录下创建templatetags 目录(名称不可更改)
2、创建任意.py文件,如xxoo.py ,在文件内写自定义功能的函数。使用django.template.Library()创建register对象(对象名不可更改)
使用 @register.simple_tag 装饰自定义函数
示例xxoo.py
from django import template register = template.Library() @register.filter def plus(a1, a2): # 形参最多支持2个 return a1 + a2
3、模板中使用 {% load '书写自定义函数的文件名' %} 加载,
函数用法:{{ 参数1|函数名:参数二 }}
html中语法示例:
{{ 2|plus:1 }}
优点:可以使用if语句 缺点:定义函数时参数个数最多两个,使用时参数1|函数名:参数2 之间不能有空格
{% if 2|plus:2 > 3 %} <span>大于3</span> {% endif %}
分页功能
django.utils.safestring import make_safe 后端设置字符串为安全字符串,在模板中渲染时字符串中的html标签不做转义。浏览器可以正常解析html标签
views.py 获取指定分页值,根据分页获展示指定分页数据
from django.shortcuts import render from django.shortcuts import HttpResponse from django.views import View from app02 import models from django.utils.safestring import mark_safe # Create your views here. LIST = range(1, 101) class PageTest(View): def get(self,request): # 获取当前页码 current_page = int(request.GET.get('p', 1)) # 单页显示数量 page_size = request.GET.get('size', 10) start = (current_page - 1) * page_size end = current_page * page_size # 当前页显示的数据 data = LIST[start:end] # 分页个数逻辑,根据数据总数/每页显示个数计算 all_count = len(LIST) count, y = divmod(all_count, page_size) # 如果余数不为零 则需要加一页显示 if y: count += 1 # 分页标签html字符串 page_list = [] for i in range(1, count + 1): if current_page == i: temp = '<a class="page active" href="/database_study/page-test/?p=%s">%s</a>' % (i, i) else: temp = '<a class="page " href="/database_study/page-test/?p=%s">%s</a>' % (i, i) page_list.append(temp) # 将列表中每个分页html字符串相连接 page_str = ''.join(page_list) # 将字符串设置为安全字符串from django.utils.safestring import mark_safe page_str = mark_safe(page_str) return render(request, 'page_test.html', {'li': data, 'page_str': page_str})
分页数量过多,分页标签只展示部分。
示例代码:
from django.shortcuts import render from django.shortcuts import HttpResponse from django.views import View from app02 import models from django.utils.safestring import mark_safe # Create your views here. LIST = [] for i in range(1, 1001): LIST.append({'key': 'value' + str(i)}) class PageTest(View): def get(self,request): # 获取当前页码 current_page = int(request.GET.get('p', 1)) # 单页显示数量 page_size = int(request.GET.get('size', 10)) # 当前页显示的数据 start = (current_page - 1) * page_size end = current_page * page_size data = LIST[start:end] # 分页个数计算,根据数据总数/每页显示个数计算 all_count = len(LIST) page_count, y = divmod(all_count, page_size) # 如果余数不为零 则需要加一页显示 if y: page_count += 1 # 每页显示几个分页标签 page_num = 7 # 分页标签html字符串 page_list = [] # 当前分页标签居中,计算起始和结束分页标签 # 分页数量不足设定的最少标签(本例:7个)的时候,起始为第一页,结束为分页总数 if page_count < page_num: start_index = 1 end_index = page_count + 1 # 分页大于一次性显示的分页标签时 else: # 判断当前页位置,如果在设定显示页数的最前面1/2时,起始和结束分页标签的位置 if current_page <= (page_num + 1)/2: start_index = 1 end_index = page_num + 1 # 当前也在中间或者总分页最后几页时,计算起始和结束标签页 else: # 当前页在总页数 - 单页显示标签数1/2之间时,避免超出总分页,计算起始和结束页码 if current_page + (page_num + 1)/2 > page_count: end_index = page_count +1 start_index = page_count - page_num + 1 # 当前页码在中间位置,前后都没有超出页码范围,计算起始和结束标签 else: start_index = current_page - (page_num -1)/2 end_index = current_page + (page_num + 1)/2 start_index = int(start_index) end_index = int(end_index) # 上一页标签 if current_page == 1: prev_page = '<a class="page " href="javascript:void(0);">上一页</a>' else: prev_page = '<a class="page " href="/database_study/page-test/?p=%s&size=%s">上一页</a>' % ((current_page - 1), page_size) page_list.append(prev_page) for i in range(start_index, end_index): if current_page == i: temp = '<a class="page active" href="/database_study/page-test/?p=%s&size=%s">%s</a>' % (i, page_size, i) else: temp = '<a class="page " href="/database_study/page-test/?p=%s&size=%s">%s</a>' % (i, page_size, i) page_list.append(temp) # 下一页标签 if current_page == page_count: next_page = '<a class="page " href="javascript:void(0);">下一页</a>' else: next_page = '<a class="page " href="/database_study/page-test/?p=%s&size=%s">下一页</a>' % ((current_page + 1), page_size) page_list.append(next_page) # 将列表中每个分页html字符串相连接 page_str = ''.join(page_list) # 将字符串设置为安全字符串from django.utils.safestring import mark_safe page_str = mark_safe(page_str) return render(request, 'page_test.html', {'li': data, 'page_str': page_str})
html 文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .pagination .page { display: inline-block; padding: 5px; margin: 5px; background-color: #6DDCBD; } .pagination .page.active { background-color: brown; color: white; } </style> </head> <body> <ul> {% for item in li %} <li>{{ item }} </li> {% endfor %} </ul> <div class="pagination"> {{ page_str }} </div> </body> </html>
面向对象方式,封装分页功能
from django.shortcuts import render from django.shortcuts import HttpResponse from django.views import View from app02 import models from django.utils.safestring import mark_safe # Create your views here. # 模拟 数据库获取到千余条数据 LIST = [] for i in range(1, 1001): LIST.append({'key': 'value' + str(i)}) class CalcPageTag(object): def __init__(self,page_size, data_count, current_page, page_num, ): self.data_count = data_count self.page_size = page_size self.current_page = current_page self.page_num = page_num def start(self): start_data_index = (self.current_page - 1) * self.page_size return start_data_index def end(self): end_data_index = self.current_page * self.page_size return end_data_index def page_count(self): # 分页个数计算,根据数据总数/每页显示个数计算 count_num, y = divmod(self.data_count, self.page_size) # 如果余数不为零 则需要加一页显示 if y: count_num += 1 return count_num def page_tag(self): page_count = self.page_count() # 当前分页标签居中,计算起始和结束分页标签 # 分页数量不足设定的最少标签(本例:7个)的时候,起始为第一页,结束为分页总数 if page_count < self.page_num: start_index = 1 end_index = page_count + 1 # 分页大于一次性显示的分页标签时 else: # 判断当前页位置,如果在设定显示页数的最前面1/2时,起始和结束分页标签的位置 if self.current_page <= (self.page_num + 1)/2: start_index = 1 end_index = self.page_num + 1 # 当前也在中间或者总分页最后几页时,计算起始和结束标签页 else: # 当前页在总页数 - 单页显示标签数1/2之间时,避免超出总分页,计算起始和结束页码 if self.current_page + (self.page_num + 1)/2 > page_count: end_index = page_count + 1 start_index = page_count - self.page_num + 1 # 当前页码在中间位置,前后都没有超出页码范围,计算起始和结束标签 else: start_index = self.current_page - (self.page_num - 1)/2 end_index = self.current_page + (self.page_num + 1)/2 start_index = int(start_index) end_index = int(end_index) return start_index, end_index def page_tag_str(self,base_url): # 分页标签html字符串 page_list = [] start_index, end_index = self.page_tag() # 上一页标签 if self.current_page == 1: prev_page = '<a class="page " href="javascript:void(0);">上一页</a>' else: prev_page = '<a class="page" href="%s?p=%s&size=%s">上一页</a>' % (base_url, (self.current_page - 1), self.page_size) page_list.append(prev_page) # 分页标签html字符串 for i in range(start_index, end_index): if self.current_page == i: temp = '<a class="page active" href="%s?p=%s&size=%s">%s</a>' % (base_url, i, self.page_size, i) else: temp = '<a class="page " href="%s?p=%s&size=%s">%s</a>' % (base_url, i, self.page_size, i) page_list.append(temp) # 下一页标签 if self.current_page == self.page_count(): next_page = '<a class="page " href="javascript:void(0);">下一页</a>' else: next_page = '<a class="page " href="%s?p=%s&size=%s">下一页</a>' % (base_url, (self.current_page + 1), self.page_size) page_list.append(next_page) # 将列表中每个分页html字符串相连接 page_str = ''.join(page_list) # 将字符串设置为安全字符串from django.utils.safestring import mark_safe page_str = mark_safe(page_str) return page_str class PageTest(View): def get(self,request): # 获取当前页码 current_page = int(request.GET.get('p', 1)) # 单页显示数量 page_size = int(request.GET.get('size', 10)) # 显示几个分页标签 page_num = 7 # 实例化自定义数据范围&标签计算类。 page = CalcPageTag(page_size=page_size, data_count=len(LIST), current_page=current_page,page_num=page_num) # 当前页显示的数据 data = LIST[page.start():page.end()] tag_base_url = '/database_study/page-test/' page_str = page.page_tag_str(tag_base_url) return render(request, 'page_test.html', {'li': data, 'page_str': page_str})
备注: <a class="page " href="javascript:void(0);">上一页</a> 标签 中 href="javascript:void(0)表示什么都不做
posted on 2020-07-18 17:36 zhangmingda 阅读(809) 评论(0) 编辑 收藏 举报