Python 【第十一章】 Django模版
1、直接传值
urls.py
"""mysite URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from cmdb import views
from django.conf.urls import url, include urlpatterns = [
# url(r'^web/', include('cmdb.urls')),
url(r'^template/',views.template),
]
views.py
def template(request):
return render(request,'template.html',
{'k1':'v1',
'k2':[11,22,33,44,55],
'k3':{'nid':12,'name':'alex'}})
template.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ k1 }}
{{ k2.0 }}
{{ k3.nid }} </body>
</html>
备注:例如以上单值传递时 索引 要使用 点 再加 一个索引值来取值。
上面列表通过for循环来取值
template.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ k1 }}
{{ k2.0 }}
{{ k3.nid }} {% for item in k2 %}
<p>{{ item }}</p>
{% endfor %}
</body>
</html>

一些例子
重复输出item内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ k1 }}
{{ k2.0 }}
{{ k3.nid }} {% for item in k2 %}
<p>{{ item }},{{ item }}</p>
{% endfor %}
</body>
</html>

输出循环序号
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ k1 }}
{{ k2.0 }}
{{ k3.nid }} {% for item in k2 %}
<p>{{ item }},{{ forloop.counter }}</p>
{% endfor %}
</body>
</html>

循环序号从0开始
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ k1 }}
{{ k2.0 }}
{{ k3.nid }} {% for item in k2 %}
<p>{{ item }},{{ forloop.counter }} {{ forloop.counter0 }}</p>
{% endfor %}
</body>
</html>

循环取第一个,或最后一个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ k1 }}
{{ k2.0 }}
{{ k3.nid }} {% for item in k2 %}
<p>{{ item }},{{ forloop.counter }}, {{ forloop.counter0 }},{{ forloop.first }},{{ forloop.last }}</p>
{% endfor %}
</body>
</html>

2、判断语句在模版中
{% if 1 == 1 %}
<h1>123</h1>
{% endif %}
{% if k1 == 'v1' %}
<h1>V1</h1>
{% elif k2 == 'v2' %}
<h1>V2</h1>
{% else %}
<h1>777</h1>
{% endif %}
模版语言内置方法(Django自带函数)
{{ item.event_start|date:"Y-m-d H:i:s"}} #时间格式
{{ bio|truncatewords:"30" }} #显示前30个字符
{{ my_list|first|upper }} #字符串第一个字符大写
{{ name|lower }} #字符串变小字
模版语言自定义方法
filter
simple_tag

1、创建指定文件,名称不能改 templatetags
2、创建任意 .py 文件,如:xx.py from django import template
from django.utils.safestring import mark_safe
#下面一句 在3.5.2要屏蔽,否则不能正常运行自定义方法#
#from django.template.base import resolve_variable, Node, TemplateSyntaxError
# 必须不变
register = template.Library() # 创建函数
@register.filter
def f1(value):
return value + "666"
3、在html模版的头部执行 {% load xx %} 4、
k1 = 'VVV'
{{k1}} => vvv #在html中生成 对应值
{{k1|f1}} => vvv666 5、 settings.py 中注册app 总结:
filter
限制:传参
支持:模版语言的 if 条件
simple_tag
不支持:模版语言的 if 条件
备注:
filter 只能传递一个参数。 例如
xx.py
#!/usr/bin/env python
#coding:utf-8
from django import template
from django.utils.safestring import mark_safe
# from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.filter
def f1(value,arg):
return value + ''+arg
html模版文件中
{% load xx %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
#传入alex参数
{{ k1 | f1:'alex' }}
</body>
</html>
simple_tag用法
xx.py
装饰器修改为
simple_tag
@register.simple_tag
def f2(value,arg):
return value + ''+arg
html模版中可以传入多个参数
{% load xx %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% f2 '1' '2' %}
</body>
</html>

fitler 可以使用if条件判断
在html模版中
注意k1与f3之间管道不要有空格
{% if k1|f3 %}
<h1>True</h1>
{% else %}
<h1>False</h1>
{% endif %}
xx.py
@register.filter
def f3(value):
if value=="VVV":
return True
return False

母版:
母版html文件:loyout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="..." />
<style>
.pg-header{
height: 48px;
background-color: cadetblue;
}
.pg-body{
min-height: 500px;
}
.pg-body .body-menu{
width: 20%;
float: left;
}
.pg-body .body-content{
width: 80%;
float: left;
}
.pg-footer{
height: 100px;
background-color: brown;
}
.active{
background-color: aquamarine;
color: white;
}
</style> {% block css %}{% endblock %}
</head>
<body>
<div class="pg-header">
后台系统V1
</div>
<div class="pg-body">
<div class="body-menu">
<ul>
<li><a id="userinfo" href="/web/userinfo">用户管理</a></li>
<li><a id="assets" href="/web/assets">资产管理</a></li>
</ul>
</div>
<div class="body-content"> {% block body %}{% endblock %}
</div> </div>
<div class="pg-footer"></div>
<script src="xxx"></script>
{% block js %}{% endblock %}
</body>
</html>
导入母版html文件
{% extends 'layout.html' %}
{% block css %}
<style></style>
{% endblock %}
{% block body %}
<ul>
{% for item in user_list %}
<li>{{ item.username }},{{ item.salary }}</li>
{% endfor %}
</ul>
{% endblock %}
{% block js %}
<script>
document.getElementById('userinfo').className = 'active';
</script>
{% endblock %}
备注:做母版中重要以下几部分
{% block css %}{% endblock %} #允许子版自己定义css
{% block body %}{% endblock %} #子版实际内容
{% block js %}{% endblock %} #允许子版 自己定义js
在子版中 以下格式导入母版
{% extends 'layout.html' %}
{% block css %}
#子版自己定义CSS
{% endblock %}
{% block body %}
# 子版内容
{% endblock %}
{% block js %}
#子版自己定义js
{% endblock %}
Python 【第十一章】 Django模版的更多相关文章
- 第二十一章 Django的分页与cookie
第二十一章 Django的分页与cookie 第一课 模板 1.模板的继承 在Template目录下新建模板master.html <!DOCTYPE html> <html lan ...
- Python第十一章-常用的核心模块01-collections模块
python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...
- Python第十一章-常用的核心模块03-json模块
python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...
- Python第十一章-常用的核心模块04-datetime模块
python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...
- 流畅的python第十一章接口学习记录
鸭子协议(忽略对象真正类型,转而关注对象有没有实现所需的方法,签名和语义) 标准库中的抽象基类 collections.abc模块中的抽象基类 抽象方法是抽象基类中用来强制子类必须实现的方法,如果子类 ...
- 《Django By Example》第十一章 中文 翻译 (个人学习,渣翻)
第十一章 缓存内容 (译者 @ucag 注:这是倒数第二章,最后一个项目即将完成. @夜夜月 将会接过翻译的最后一棒.这本书的翻译即将完成.这也是我翻译的最后一章,作为英语专业的学生,我对于翻译有了更 ...
- python 教程 第二十一章、 扩展Python
第二十一章. 扩展Python /* D:\Python27\Lib\Extest-1.0\Extest2.c */ #include <stdio.h> #include <std ...
- 第十一章:Python高级编程-协程和异步IO
第十一章:Python高级编程-协程和异步IO Python3高级核心技术97讲 笔记 目录 第十一章:Python高级编程-协程和异步IO 11.1 并发.并行.同步.异步.阻塞.非阻塞 11.2 ...
- 第三章:模版层 - 1:Django模板语言详解
本节将介绍Django模版系统的语法.Django模版语言致力于在性能和简单性上取得平衡. 如果你有过其它编程背景,或者使用过一些在HTML中直接混入程序代码的语言,那么你需要记住,Django的模版 ...
随机推荐
- Hibernate(1)——数据访问层的架构模式
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: 数据库的概念.逻辑.数据模型概念 应用程序的分层体系结构发展 MVC设计模式与四层结构的对应关系 持久层的设 ...
- web前端性能调优
最近2个月一直在做手机端和电视端开发,开发的过程遇到过各种坑.弄到快元旦了,终于把上线了.2个月干下来满满的的辛苦,没有那么忙了自己准备把前端的性能调优总结以下,以方便以后自己再次使用到的时候得于得心 ...
- HTTPS那些事(一)HTTPS原理
转载来自:http://www.guokr.com/post/114121/ 谣言粉碎机前些日子发布的<用公共WiFi上网会危害银行账户安全吗?>,文中介绍了在使用HTTPS进行网络加密传 ...
- WebComponent魔法堂:深究Custom Element 之 从过去看现在
前言 说起Custom Element那必然会想起那个相似而又以失败告终的HTML Component.HTML Component是在IE5开始引入的新技术,用于对原生元素作功能"增强& ...
- 精彩 JavaScript 代码片段
1. 根据给定的条件在原有的数组上,得到所需要的新数组. ——<JavaScript 王者归来> var a = [-1,-1,1,2,-2,-2,-3,-3,3,-3]; functio ...
- STM32F746的RTC使用
1.RTC模块采用低速晶振外接始终:32.768KHz,如下图所示 2.配置RTC模块: 其中,Fck_apre.Fck_spre始终上配置不容易理解, 如果想得到1Hz的始终频率,则需要将PERDI ...
- C#连接Access与SQL Server
1.连接Access数据库 string strConnection = "Provider=Microsoft.Ace.OleDb.12.0; Data Source=" + S ...
- Spring下ActiveMQ实战
MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...
- 使用MyBatis Generator自动创建代码(dao,mapping,poji)
连接的数据库为SQL server2008,所以需要的文件为sqljdbc4.jar 使用的lib库有: 在lib库目录下新建一个src文件夹用来存放生成的文件,然后新建generatorConfig ...
- Using AlloyTouch to control three.js 3D model
As you can see, the above cube rotation, acceleration, deceleration stop all through the AlloyTouch ...
