django之template
django模板
1、Django 模版基本语法
>>> from django.template import Context, Template
>>> t = Template('My name is {{ name }}.')
>>> c = Context({'name': 'Stephane'})
>>> t.render(c)
u'My name is Stephane.'
同一模板,多个上下文
一旦有了 模板 对象,你就可以通过它渲染多个context, 例如:
>>> from django.template import Template, Context
>>> t = Template('Hello, {{ name }}')
>>> print t.render(Context({'name': 'John'}))
Hello, John
>>> print t.render(Context({'name': 'Julie'}))
Hello, Julie
>>> print t.render(Context({'name': 'Pat'}))
Hello, Pat
无论何时我们都可以像这样使用同一模板源渲染多个context,只进行 一次模板创建然后多次调用render()方法渲染会更为高效:
# Bad
for name in ('John', 'Julie', 'Pat'):
t = Template('Hello, {{ name }}')
print t.render(Context({'name': name}))
# Good
t = Template('Hello, {{ name }}')
for name in ('John', 'Julie', 'Pat'):
print t.render(Context({'name': name}))
Django 模板解析非常快捷。 大部分的解析工作都是在后台通过对简短正则表达式一次性调用来完成。 这和基于 XML 的模板引擎形成鲜明对比,那些引擎承担了 XML 解析器的开销,且往往比 Django 模板渲染引擎要慢上几个数量级。
2、深度变量的查找
在到目前为止的例子中,我们通过 context 传递的简单参数值主要是字符串,然而,模板系统能够非常简洁地处理更加复杂的数据结构,例如list、dictionary和自定义的对象。
在 Django 模板中遍历复杂数据结构的关键是句点字符 (.)。
最好是用几个例子来说明一下。 比如,假设你要向模板传递一个 Python 字典。 要通过字典键访问该字典的值,可使用一个句点:
>>> from django.template import Template, Context
>>> person = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c = Context({'person': person})
>>> t.render(c)
u'Sally is 43 years old.'
同样,也可以通过句点来访问对象的属性。 比方说, Python 的 datetime.date 对象有 year 、 month 和 day 几个属性,你同样可以在模板中使用句点来访问这些属性:
>>> from django.template import Template, Context
>>> import datetime
>>> d = datetime.date(1993, 5, 2)
>>> d.year
1993
>>> d.month
5
>>> d.day
2
>>> t = Template('The month is {{ date.month }} and the year is {{ date.year }}.')
>>> c = Context({'date': d})
>>> t.render(c)
u'The month is 5 and the year is 1993.'
这个例子使用了一个自定义的类,演示了通过实例变量加一点(dots)来访问它的属性,这个方法适用于任意的对象。
>>> from django.template import Template, Context
>>> class Person(object):
... def __init__(self, first_name, last_name):
... self.first_name, self.last_name = first_name, last_name
>>> t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')
>>> c = Context({'person': Person('John', 'Smith')})
>>> t.render(c)
u'Hello, John Smith.'
点语法也可以用来引用对象的* 方法*。 例如,每个 Python 字符串都有 upper() 和 isdigit() 方法,你在模板中可以使用同样的句点语法来调用它们:
>>> from django.template import Template, Context
>>> t = Template('{{ var }} -- {{ var.upper }} -- {{ var.isdigit }}')
>>> t.render(Context({'var': 'hello'}))
u'hello -- HELLO -- False'
>>> t.render(Context({'var': '123'}))
u'123 -- 123 -- True'
注意这里调用方法时并 没有 使用圆括号 而且也无法给该方法传递参数;你只能调用不需参数的方法。 (我们将在本章稍后部分解释该设计观。)**
最后,句点也可用于访问列表索引,例如:
>>> from django.template import Template, Context
>>> t = Template('Item 2 is {{ items.2 }}.')
>>> c = Context({'items': ['apples', 'bananas', 'carrots']})
>>> t.render(c)
u'Item 2 is carrots.'
如果模板是html,如何传变量到html文件中,如下:<见D:\OneDrive\python35\y_16\y_16\templates\app01\inde.html>
<ul>
{% for user_obj in user_objs %}
{# 下面if用来判断字符串是否相等#}
{# {% if user_obj.username == 'mofei3' %}#}
{# 下面if用来判断列表序号,即数字大小判断,也有内置方法forloop.counter0,是从下标0开始#}
{# {% if forloop.counter > 2 %}#}
{# 下面if用来判断列表序号,即数字大小能否被数字整除,比如2#}
{% if forloop.counter|divisibleby:"2" %}
<li style="background-color: red">username:{{ user_obj.username }}, name:{{ user_obj.name }}}</li>
{% else %}
<li style="">username:{{ user_obj.username }}, name:{{ user_obj.name }}}</li>
{% endif %}
{% endfor %}
</ul>
对应view.index方法如下:
def index(request):
if request.method == "GET":
user_infos = [
{'username':'mofei','name':'Mofei'},
{'username':'mofei2','name':'Mofei2'},
{'username':'mofei3','name':'Mofei3'},
{'username':'mofei4','name':'Mofei4'}
]
return render(request, 'app01/index.html',{'user_objs':user_infos})
else:
return 111
3、模板的继承和复用
模板继承
在要继承的子页面首行添加{% extends 'app01/index.html' %}
例:
如果子页面page1.html要继承父页面index.html的页眉和页尾,则如下:
增加app01/page1.html文件
{#继承父页面#}
{% extends 'app01/index.html' %}
{#不继承,即要重写的页面如下:#}
{% block header-menu %}
<h1>sub menu 1..2..3..4..5</h1>
{% endblock %}
{% block content-container %}
page1
{% endblock %}
对要修改的内容,还要在父页面index.html添加{% block xxxxxxx %}和{%endblock}
<body>
<ul >
<li style="display: inline-block">Home</li>
<li style="display: inline-block">page1</li>
<li style="display: inline-block">page2</li>
<li style="display: inline-block">page3</li>
</ul>
{% block content-container %}
<ul>
{% for user_obj in user_objs %}
{# 下面if用来判断字符串是否相等#}
{# {% if user_obj.username == 'mofei3' %}#}
{# 下面if用来判断列表序号,即数字大小判断,也有内置方法forloop.counter0,是从下标0开始#}
{# {% if forloop.counter > 2 %}#}
{# 下面if用来判断列表序号,即数字大小能否被数字整除,比如2#}
{% if forloop.counter|divisibleby:"2" %}
<li style="background-color: red">username:{{ user_obj.username }}, name:{{ user_obj.name }}}</li>
{% else %}
<li style="">username:{{ user_obj.username }}, name:{{ user_obj.name }}}</li>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
<footer>
<p>==================这是页尾==============</p>
</footer>
模板引用
只需要在html页面内插入include语句,如:
{% include 'app01/xxx.html' %}
多层继承
-
父页面可以继承爷页面,子页面也可以继承父页面
-
如果子页面继承父页面之后,重写了某个block,则是重写了爷页面(当然,爷页面也存在此block),而不是父页面
-
如果,子页面要重写父页面而不是爷页面,则需要在父页面单独定义block
不支持一个html文件同时继承多个页面
本文来自博客园,作者:鬼凤,转载请注明原文链接:https://www.cnblogs.com/microfan/articles/5727162.html