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模版的更多相关文章

  1. 第二十一章 Django的分页与cookie

    第二十一章 Django的分页与cookie 第一课 模板 1.模板的继承 在Template目录下新建模板master.html <!DOCTYPE html> <html lan ...

  2. Python第十一章-常用的核心模块01-collections模块

    python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...

  3. Python第十一章-常用的核心模块03-json模块

    python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...

  4. Python第十一章-常用的核心模块04-datetime模块

    python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...

  5. 流畅的python第十一章接口学习记录

    鸭子协议(忽略对象真正类型,转而关注对象有没有实现所需的方法,签名和语义) 标准库中的抽象基类 collections.abc模块中的抽象基类 抽象方法是抽象基类中用来强制子类必须实现的方法,如果子类 ...

  6. 《Django By Example》第十一章 中文 翻译 (个人学习,渣翻)

    第十一章 缓存内容 (译者 @ucag 注:这是倒数第二章,最后一个项目即将完成. @夜夜月 将会接过翻译的最后一棒.这本书的翻译即将完成.这也是我翻译的最后一章,作为英语专业的学生,我对于翻译有了更 ...

  7. python 教程 第二十一章、 扩展Python

    第二十一章. 扩展Python /* D:\Python27\Lib\Extest-1.0\Extest2.c */ #include <stdio.h> #include <std ...

  8. 第十一章:Python高级编程-协程和异步IO

    第十一章:Python高级编程-协程和异步IO Python3高级核心技术97讲 笔记 目录 第十一章:Python高级编程-协程和异步IO 11.1 并发.并行.同步.异步.阻塞.非阻塞 11.2 ...

  9. 第三章:模版层 - 1:Django模板语言详解

    本节将介绍Django模版系统的语法.Django模版语言致力于在性能和简单性上取得平衡. 如果你有过其它编程背景,或者使用过一些在HTML中直接混入程序代码的语言,那么你需要记住,Django的模版 ...

随机推荐

  1. Oracle 11g静默安装简明版

    环境:RHEL 6.5 + Oracle 11.2.0.4 1. 初步处理应答文件 2. 静默安装软件 3. 静默安装监听 4. 静默dbca建库 说明: 本文默认安装软件前的步骤已经设置完毕 如果没 ...

  2. 设置eclipse中自动添加get,set的注释为字段属性的注释

    一:说明 首先具体来看一下是什么效果,上图可能会更清楚一点 就是在get/set中自动加上属性的注释,那我们要怎么配置呢? 二:配置 2.1:下载附件 下载附件 2.2:替换class 原生的ecli ...

  3. ASP.NET Core 中文文档 第二章 指南(4.5)使用 SQL Server LocalDB

    原文:Working with SQL Server LocalDB 作者:Rick Anderson 翻译: 魏美娟(初见) 校对: 孟帅洋(书缘).张硕(Apple).许登洋(Seay) Appl ...

  4. Bloom Filter:海量数据的HashSet

    Bloom Filter一般用于数据的去重计算,近似于HashSet的功能:但是不同于Bitmap(用于精确计算),其为一种估算的数据结构,存在误判(false positive)的情况. 1. 基本 ...

  5. AC自动机-算法详解

    What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...

  6. 『.NET Core CLI工具文档』(十一)dotnet-test

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-test 翻译:dotnet-test 名称 dotnet-test - 使用配置的测试运行器运行单元测试 ...

  7. c#面向对象基础技能——学习笔记(二)基于OOP思想研究对象的【属性】

    字段(成员变量): 字段只能从对象中访问实例字段,无法直接从类中访问(换言之,不创建实例就不能访问),可以理解为:字段一般用在内部数据交互使用,当需要为外部提供数据时,(要优先使用自动实现的属性而不是 ...

  8. Android 的进程和线程

    进程和线程 如果某个应用程序组件是第一次被启动,且这时应用程序也没有其他组件在运行,则android系统会为应用程序创建一个包含单个线程的linux进程.默认情况下,同一个应用程序的所有组件都运行在同 ...

  9. 浅谈Slick(3)- Slick201:从fp角度了解Slick

    我在上期讨论里已经成功的创建了一个简单的Slick项目,然后又尝试使用了一些最基本的功能.Slick是一个FRM(Functional Relational Mapper),是为fp编程提供的scal ...

  10. Java-加载数据库驱动,取得数据库连接

    在Java中想要进行数据库操作,最重要的两个步骤就是加载数据驱动,然后取得数据库连接. 1.加载 数据库驱动( Class.forName(String className) ): 因为Java是一种 ...