4.Django模板语言和分页
继承 extends
子版只能继承一个父模板
1.父模板 master.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>{% block title %} {% endblock %}</title>
- <link rel="stylesheet" href="/static/common.css">
- <style>
- .pg-header{
- height: 50px;
- background-color: red;
- color:blue
- }
- {% block css %} {% endblock %}
- </style>
- </head>
- <body>
- <div class="pg-header">小男孩管理</div>
- {% block content %} {% endblock %}
- <div class="pg-footer"></div>
- <script src="/static/jquery-1.12.4.js"></script>
- {% block js %} {% endblock %}
- </body>
- </html>
2.子版继承方法
- {% extends 'master.html' %} #引用母版
- {% block title %}用户管理{% endblock %}
- {% block content %}
- <h1>用户管理</h1>
- <ul>
- {% for i in u %}
- <li>{{ i }}</li>
- {% endfor %}
- </ul>
- {% endblock %}
- {% block css %}
- <style>
- body{
- background-color: black;
- }
- </style>
- {% endblock %}
- {% block js %}
- <script>
- </script>
- {% endblock %}
导入定制的组件 include
创建tag.html
在index.html中导入tag.html,可以导入很多个
- {% include 'tag.html' %}
tag.html
- form>
- <input type="text" name="user"/>
- <input type="submit" value="提交"/>
- </form>
index.html
- {# 指定继承的模板 #}
- {% extends 'master.html' %}
- {# 指定替换的位置 #}
- {% block title %}
- tp1
- {% endblock %}
- {# 指定替换的位置 #}
- {% block content %}
- <p>tp1</p>
- {# 导入单独组件 #}
- {% include 'tag.html' %}
- {% endblock %}
simple_tag and filter
1.django默认自带方法
- {{ item.event_start|date:"Y-m-d H:i:s"}} #日期格式进行转换
- {{ bio|truncatewords:"30" }} #取字符串前30位
- {{ my_list|first|upper }} #第一个字符大写
- {{ name|lower }} #所有字符小写
2.simple_tag
- 第一步: 在app01下面创建templatetags(必须是这个名字)文件夹
- 第二步:在templatetags下面创建test1.py文件
- 第三步:模板中 首先在开头要先导入 {% load test1 %}
- 第四步: 模板中使用方法 {% 函数名 参数1 参数2 %}
test1.py
- from django import template
- from django.utils.safestring import mark_safe
- # 必须是register对象
- register = template.Library()
@register.simple_tag- def func(a1,a2):
- return a1 + a2
index.py
- {% load test1 %}
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- {{ name }}
- {{ name|lower }}
- {% func 2 5 %}
- </body>
- </html>
3.filter
test1.py
- from django import template
- from django.utils.safestring import mark_safe
- # 必须是register对象
- register = template.Library()
@register.simple_tag- def func(a1,a2):
- return a1 + a2
- @register.filter()
- def func1(b1,b2):
- return b1 + b2
index.py
- {% load test1 %}
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- {{ name }}
- {{ name|lower }}
- {# simpletag#}
- {% func 2 5 %}
- {# filter#}
- {{ 'zhang'|func1:'derek' }}
- </body>
- </html>
总结:
- simple:
- 优点:参数任意
- 缺点:不能作为if条件
- filter
- 优点:最多两个参数
- 缺点:可以作为if条件
分页
1.简单分页
涉及xss攻击,需要用到mark_safe方法,使用此方法字符串传输到后端后,已html形式显示,而非字符串
HTML文件
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style>
- .pagination .page{
- display: inline-block;
- padding: 5px;
- background-color: cyan;
- margin: 5px;
- }
- .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>
user_list.html
views.py
- from django.shortcuts import render,HttpResponse
- from django.core.handlers.wsgi import WSGIRequest
- from django.utils.safestring import mark_safe
- LIST = []
- for i in range(109):
- LIST.append(i)
- from django.utils.safestring import mark_safe
- def user_list(request):
- current_page = request.GET.get('p',1)
- current_page = int(current_page)
- start = (current_page-1)*10
- end = current_page*10
- data = LIST[start:end]
- all_count = len(LIST)
- count,y = divmod(all_count,10)
- if y :
- count +=1
- page_list = []
- for i in range(1,count+1):
- if i == current_page:
- temp = '<a class="page active" href="/user_list/?p=%s">%s</a>'%(i,i)
- else:
- temp = '<a class="page" href="/user_list/?p=%s">%s</a>'%(i,i)
- page_list.append(temp)
- page_str = mark_safe(''.join(page_list))
- return render(request,'user_list.html',{'li':data,'page_str':page_str})
浏览器访问地址
- 浏览器访问地址:http://127.0.0.1:8000/user_list/?p=3
2.增加功能
分页数进行定制,添加上一页、下一页,增加跳转功能,实现分页的完整功能
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style>
- .pagination .page{
- display: inline-block;
- padding: 5px;
- background-color: cyan;
- margin: 5px;
- }
- .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>
- user_list.html
user_list
views.py
- LIST = []
- for i in range(199):
- LIST.append(i)
- from django.utils.safestring import mark_safe
- def user_list(request):
- current_page = request.GET.get('p',1)
- current_page = int(current_page)
- start = (current_page-1)*10
- end = current_page*10
- data = LIST[start:end]
- all_count = len(LIST)
- total_count,y = divmod(all_count,10)
- if y :
- total_count +=1
- pager_num = 11 #页码数
- page_list = []
- if total_count < pager_num : #总页面小于页码数
- start_index = 1
- end_index = total_count + 1
- else:
- if current_page <= pager_num/2: #开头
- start_index = 1
- end_index = pager_num + 1
- elif current_page + (pager_num-1)/2 >= total_count: #中间
- start_index = total_count - (pager_num-1)
- end_index = total_count + 1
- else: #结尾
- start_index = current_page - (pager_num-1)/2
- end_index = current_page + (pager_num-1)/2 + 1
- # 上下页码
- if current_page == 1:
- prev = '<a class="page" href="javascript:void(0)">上一页</a>' # 什么都不干
- else:
- prev = '<a class="page" href="/user_list/?p=%s">上一页</a>'%(current_page-1)
- page_list.append(prev)
- for i in range(int(start_index),int(end_index)):
- if i == current_page:
- temp = '<a class="page active" href="/user_list/?p=%s">%s</a>'%(i,i)
- else:
- temp = '<a class="page" href="/user_list/?p=%s">%s</a>'%(i,i)
- page_list.append(temp)
- if current_page == total_count:
- nex = '<a class="page" href="javascript:void(0)">下一页</a>' # 什么都不干
- else:
- nex = '<a class="page" href="/user_list/?p=%s">下一页</a>'%(current_page+1)
- page_list.append(nex)
- # 跳转 可以写到前端
- jump = '''
- <input type="text" /><a onclick="jumpTo(this,'/user_list/?p=');">GO</a>
- <script>
- function jumpTo(ths,base) {
- var val = ths.previousSibling.value;
- location.href = base + val;
- }
- </script>
- '''
- page_list.append(jump)
- page_str = mark_safe(''.join(page_list))
- return render(request,'user_list.html',{'li':data,'page_str':page_str})
3.优化完善
页码代码跟业务代码分开,创建class类,然后views导入进去
app01下面创建 utils文件夹,里面创建pagination.py
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style>
- .pagination .page{
- display: inline-block;
- padding: 5px;
- background-color: cyan;
- margin: 5px;
- }
- .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>
- user_list.html
user_list.html
views.py
- LIST = []
- for i in range(199):
- LIST.append(i)
- class Page:
- def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):
- self.current_page = current_page
- self.data_count = data_count
- self.per_page_count = per_page_count
- self.pager_num = pager_num
- @property
- def start(self):
- return (self.current_page - 1) * self.per_page_count
- @property
- def end(self):
- return self.current_page * self.per_page_count
- @property
- def total_count(self):
- v, y = divmod(self.data_count, self.per_page_count)
- if y:
- v += 1
- return v
- def page_str(self, base_url):
- page_list = []
- if self.total_count < self.pager_num:
- start_index = 1
- end_index = self.total_count + 1
- else:
- if self.current_page <= (self.pager_num + 1) / 2:
- start_index = 1
- end_index = self.pager_num + 1
- else:
- start_index = self.current_page - (self.pager_num - 1) / 2
- end_index = self.current_page + (self.pager_num + 1) / 2
- if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
- end_index = self.total_count + 1
- start_index = self.total_count - self.pager_num + 1
- if self.current_page == 1:
- prev = '<a class="page" href="javascript:void(0);">上一页</a>'
- else:
- prev = '<a class="page" href="%s?p=%s">上一页</a>' % (base_url, self.current_page - 1,)
- page_list.append(prev)
- for i in range(int(start_index), int(end_index)):
- if i == self.current_page:
- temp = '<a class="page active" href="%s?p=%s">%s</a>' % (base_url, i, i)
- else:
- temp = '<a class="page" href="%s?p=%s">%s</a>' % (base_url, i, i)
- page_list.append(temp)
- if self.current_page == self.total_count:
- nex = '<a class="page" href="javascript:void(0);">下一页</a>'
- else:
- nex = '<a class="page" href="%s?p=%s">下一页</a>' % (base_url, self.current_page + 1,)
- page_list.append(nex)
- jump = """
- <input type='text' /><a onclick='jumpTo(this, "%s?p=");'>GO</a>
- <script>
- function jumpTo(ths,base){
- var val = ths.previousSibling.value;
- location.href = base + val;
- }
- </script>
- """ % (base_url,)
- page_list.append(jump)
- page_str = mark_safe("".join(page_list))
- return page_str
- from django.utils.safestring import mark_safe
- def user_list(request):
- current_page = request.GET.get('p', 1)
- current_page = int(current_page)
- page_obj = Page(current_page,len(LIST))
- data = LIST[page_obj.start:page_obj.end]
- page_str = page_obj.page_str("/user_list/")
- return render(request, 'user_list.html', {'li': data,'page_str': page_str})
4.Django模板语言和分页的更多相关文章
- 26.Django模板语言和分页
继承 extends 子版只能继承一个父模板 1.父模板 master.html <!DOCTYPE html> <html lang="en"> < ...
- 6月15日 python学习总结 Django模板语言相关内容
Django模板语言相关内容 Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} ...
- Django模板语言初识
一.Django框架简介 1.MVC框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控 ...
- Django模板语言的复用
一.include标签 由于在项目中,往往会出现多个页面拥有一个或几个相同的页面版块,或是一个页面多个页面版块是相同的,基于这个问题,我们可以采用模板语言复用include标签来帮我们解决,这样就避免 ...
- Django模板语言相关内容 Djan
Django模板语言相关内容 Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} ...
- Django——模板语言相关内容
Django模板语言相关内容 Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} ...
- Django 模板语言 路由 视图
. 模板语言(字符串替换) . 母版和继承 . 什么时候用母版? html页面有重复的代码,把它们提取出来放到一个单独的html文件. (比如:导航条和左侧菜单) . 子页面如何使用母版? {% ex ...
- django模板语言的注释
就像HTML或者Python,Django模板语言同样提供代码注释. 注释使用 {# #} : ? 1 {# This is a comment #} 注释的内容不会在模板渲染时输出. 用这种语法的注 ...
- Django模板语言(常用语法规则)
Django模板语言 The Django template language 模板中常用的语法规则 {最新版本的Django语法可能有改变,不支持的操作可能支持了.[HTML教程 - 基本元素/标签 ...
随机推荐
- 前端工具mock的使用 - 造数据模拟网络请求
前后端同步开发过程中,有时候前端页面完成了,需要等待后端接口完成部署后才能联调. 这个时候如果不想等待,想自己造数据模拟网络请求,这种情况就能用到mock工具了. mock工具可以用在web网站,也能 ...
- linux swap空间的swappiness=0
linux 会使用硬盘的一部分做为SWAP分区,用来进行进程调度--进程是正在运行的程序--把当前不用的进程调成‘等待(standby)‘,甚至‘睡眠(sleep)’,一旦要用,再调成‘活动(acti ...
- 二维条码扫描模组在肯德基KFC的无纸化点餐解决方案
在如今提倡节约资源的环境下,肯德基在品牌发展中,逐渐实现无纸化点餐,不仅节约了纸质点餐单,而且还具有节约资源的示范作用.而其中二维码扫描模组是这套无纸化点餐方案的重点,在整套设备中,加入二维码扫描模组 ...
- android studio 开发免安装的app之目录结构
尚未深入分析,暂且外链到我看到的,对此有帮助的博客,在此,感谢你们. 1.https://blog.csdn.net/tscyds/article/details/74331085 2.https:/ ...
- AI零基础入门之人工智能开启新时代—上篇
人工智能的发展史及应用 开篇:人工智能无处不在 人工智能的发展历程 · 1945艾伦图灵在论文<计算机器不智能>中提出了著名的图灵测试,给人工智能的収展产生了深远的影响. · 1951年, ...
- [转]Unity-移动设备可用的压缩解压缩源码
原文:http://www.manew.com/thread-103250-1-1.html 最近在做客户端数据的分离,不希望对项目有什么影响,也不太想用AssetBundle,太麻烦,就在网上找了找 ...
- :nth-child() 与 :nth-of-type(n)的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue(二十八)el-cascader 动态加载 - 省市区组件
1.后台接口为点击加载下一级 ,传省市区id <template> <el-cascader v-model="selectedOptions" placehol ...
- 4.28Linux(6)
2019-4-28 21:27:41 明天回家.回家继续学Linux还好有个服务器!!!感觉有个属于自己的服务器感觉好爽啊!! 越努力越幸运!永远不要高估自己!!! Nginx安装 服务器的请求原理 ...
- 【原创】XAF 常见错误以及对应解决方法
1.Appearance Criteria设置错误 Exception occurs while assigning the 'DetailView, ID:xxx_DetailView' view ...