模板层
模板层
模板语法的传值
{{}}: 变量相关
{%%}: 逻辑相关
<p>{{ n }}</p>
<p>{{ f }}</p>
<p>{{ s }}</p>
<p>{{ b }}</p>
<p>{{ l }}</p>
<p>{{ d }}</p>
<p>{{ t }}</p>
<p>{{ se }}</p>
<p>传递函数会自动加括号调用,并且返回给前端返回值,但是不能传参:{{ func }}</p>
<p>传类名的时候也会自动加括号调用实例化{{ Myclass }}</p>
<p>{{ obj }}</p>
<p>{{ obj.get_self }}</p>
<p>{{ obj.get_func }}</p>
<p>{{ obj.get_class }}</p>
"""
对象被展示到html页面上 就类似打印了,也会触发__str__方法
取值:
句点符
<p>{{ d.username }}</p>
<p>{{ l.0 }}</p>
<p>{{ d.hobby.3.info }}</p>
既可以点索引也可以点健,还可以混用
"""
def index(request):
# 模板语法可以传递的后端数据类型
n = 123
f = 1.23
s = 'qwe'
b = True
l = ['jason', 'egon']
t = (111, 222, 333, 444)
d = {'username': 'egon', 'age': 19}
se = {'qwe', 'asd', 'zcx'}
def func():
return '返回了我的执行结果'
class Myclass(object):
def get_self(self):
return 'self'
@staticmethod
def get_func():
return 'func'
@classmethod
def get_class(cls):
return 'cls'
obj = Myclass()
# return render(request, 'index.html', {}) # 可以写一个字典一个一个传
return render(request, 'index.html', locals()) # 全部传
过滤器
比较简单的内置方法
转义:
前端 |safe
后端:
from django.utils.safestring import mark_safe
res = mark_safe('<h1>qwe</h1>')
# 基本语法
{{数据 | 过滤器:参数}}
def index(request):
# 模板语法可以传递的后端数据类型
n = 123
f = 1.23
s = 'qwe'
b = True
b1 = False
l = ['jason', 'egon', 123, 132, 23, 23]
t = (111, 222, 333, 444)
d = {'username': 'egon', 'age': 19, 'hobby': [111,222,333, {'info': 'NB'}]}
se = {'qwe', 'asd', 'zcx'}
file_size = 1233441 # KB, GB,
import datetime
current_time = datetime.datetime.now()
info = '我是你爹我是你爹我是你爹我是你爹我是你爹'
egl = 'my name is lyh my age is 14 my name is lyh my age is 14 '
msg = '我 是 你 爹'
hhh = '<h1>爸爸</h1>'
def func():
return '返回了我的执行结果'
class Myclass(object):
def get_self(self):
return 'self'
@staticmethod
def get_func():
return 'func'
@classmethod
def get_class(cls):
return 'cls'
obj = Myclass()
# return render(request, 'index.html', {}) # 可以写一个字典一个一个传
return render(request, 'index.html', locals()) # 全部传
<h1>过滤器</h1>
<p>统计长度: {{ s|length }}</p>
<p>默认值: {{ b|default:'啥也不是' }}</p>
<p>默认值(第一个参数bool值是true,否则展示default后面的): {{ b1|default:'啥也不是' }}</p>
<p>文件大小: {{ file_size|filesizeformat }}</p>
<p>日期格式化: {{ current_time|date:'Y-m-d H:s:m' }}</p>
<p>切片操作,支持步长: {{ l|slice:'0:4' }}</p>
<p>切取字符(包含三个点): {{ info|truncatechars:9 }}</p>
<p>切取单词(不包含三个点 只会按照空格切): {{ egl|truncatewords:9 }}</p>
<p>移除特定的字符: {{ msg|cut:' ' }}</p>
<p>拼接: {{ l|join:'+' }}</p>
<p>拼接(加法): {{ n|add:10 }}</p>
<p>拼接(加法): {{ s|add:msg }}</p>
<p>取消转义(告诉html, 前面那个标签可以转): {{ hhh|safe }}</p>
标签
for 循环
{% for foo in l %}
<p>{{ forloop }}</p>
<p>{{ i }}</p> # 所有的元素
{% endfor %}
{'parentloop': {}, 'counter0': 0, 'counter': 1, 'revcounter': 6, 'revcounter0': 5, 'first': True, 'last': False}
if判断
{% if b %}
<p>baby</p>
{% else %}
<p>s</p>
{% else %}
<p>123</p>
{% endif %}
{#{% for foo in l %}#}
{# {% if forloop.first %}#}
{# <p>第一次循环</p>#}
{# {% elif forloop.last %}#}
{# <p>最后一次循环</p>#}
{# {% else %}#}
{# <p>{{ foo }}</p>#}
{# {% endif %}#}
{##}
{#{% endfor %}#}
{#{% for foo in d.keys %}#}
{# <p>{{ foo }}</p>#}
{#{% endfor %}#}
{##}
{#{% for foo in d.values %}#}
{# <p>{{ foo }}</p>#}
{#{% endfor %}#}
{##}
{#{% for foo in d.items %}#}
{# <p>{{ foo }}</p>#}
{#{% endfor %}#}
{% with d.hobby.3.info as nb %}
<p>起别名{{ nb }}</p>
{% endwith %}
<p>{{ d.hobby.3.info }}</p>
自定义过滤器,标签,inclusion_tag
三步走:
1.在应用下创建一个名字必须交templatetags的文件夹
2.在该文件夹内创建任意名称的py文件 eg:mytag.py
3.在该py文件内必须书写下面两句话
--from django import template
--register = template.Library()
@register.filter(name='bbb')
def my_sum(v1, v2):
return v1 + v2
{% load mytag %}
<p>{{ n|bbb:666 }}</p>
# 自定义标签(参数可以有多个)
@register.simple_tag(name='plus')
def index(a, b, c, d):
return '%s-%s-%-%s' % (a, b, c, d)
{% load mytag %}
<p>{% tplus 'jaosn' 12 3 3 %}</p>
自定义inclusing_tag
"""
内部原理:
先定义一个方法
在页面上调用该方法,并且可以传值
该方法会生成一些数据然后传给html页面
之后将渲染好的结果放到调用位置
"""
{% load mytag %}
{% left 10 %}
<ul>
{% for foo in data %}
<li>{{ foo }}</li>
{% endfor %}
</ul>
#自定义inclusion_tag
@register.inclusion_tag('left_menu.html')
def left(n):
data = ['第{}项'.format(i) for i in range(n)]
return locals() # 将data传给left_menu.html
# return {'data': data} # 第二种传值
"""
当html的某一个页面需要传参数才能动态的渲染出来,并且在多个页面上都需要使用到该局部,那么就考虑将该局部页面做成这种格式
"""
模板的继承
E:\pythonDemo\django项目\create_surface_relationship
先选好一个要继承的页面
{% extends 'home.html' %}
{% block content %}
<h1>登录页面</h1>
<form action="">
<p>username: <input type="text" name="username" class="form-control"></p>
<p>password: <input type="password" name="password" class="form-control"></p>
<input type="submit" class="btn btn-danger">
</form>
{% endblock %}
继承了页面是一养的,在继承的模板页面划定区域,子页面就可以声明划定的区域
{% block content %}
<div class="jumbotron">
<h1>Hello, world!</h1>
<p>...</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
{% endblock %}
一般情况下模板页面上至少有三块可以被修改的取余
1.css
2.html
3.js
模板的导入
将页面的某一个局部当成模块的形式
那个页面需要使用导入即可
浙公网安备 33010602011771号