模板层

模版语法传值

{{}}:里面写的变量

{%%}:里面写的逻辑

模板层可以传递的后端python数据类型:

传函数名时会自动加括号调用 ,但是模版语法不支持给函数传额外的参数

传类名的时候也会自动加括号调用(实例化)

后端views文件中:

def inde(request):
    # 模板语法可以传递的后端python数据类型
    n = 123
    f =1.23
    s = 'abc'
    b = True
    l = ['x','y','z']
    d ={'name':'hua','age':18}
    se = {'1','2','3'}
    lll = []
    # 函数
    def func():
        print('我被执行了')
        return 123
    #
    class MyClass(object):
        def get_self(self):
            return 'self'
        @staticmethod
        def get_func():
            return 'func'
        @classmethod
        def get_class(cls):
            return 'cls'
        # 对象被展示到html页面上 就类似于执行了打印操作也会触发__str__方法
        def __str__(self):
            return '到底会不会?'
    obj = MyClass() # 对象

    return render(request,'inde.html',locals()) # 一次性全都传过去

前端html文件中:

 结果:

 

总结:

传递函数名会自动加括号调用,但是模板语法不支持给函数传额外的参数

传类名的时候也会自动加括号调用(实例化)

内部能够自动判断出当前的变量名是否可以加括号调用 如果可以就会自动执行  针对的是函数名和类名

django模版语法的取值 是固定的格式 只能采用句点符: “.”  ,既可以点key也可以点索引,还能两者混用。

例如:

<p>{{ d.username }}</p>
<p>{{ l.0 }}</p>
<p>{{ d.hobby.3.info }}</p>

过滤器

过滤器就类似于是模板语法内置的内置方法

django内置有60多个过滤器 我们不需要学这么多 了解10个左右就差不多了 后面碰到了再去记忆。

过滤器的基本语法:

  {{数据|过滤器:参数}}

1.统计字符长度:

  <p>统计字符长度:{{ s|length }} </p>

2.默认值:前面有值就使用前面的,前面没有值就使用default后面的值

  <p>默认值:{{ b|default:'|前是True就是True,不是True执行我' }}</p>

3.文件大小:

  <p>文件大小:{{ file|filesizeformat }}</p>

4.日期格式化:

  <p>日期格式化:{{ current_time|date:'Y-m-d H:i:s'}}</p>

5.切片操作:

  <p>切片操作(支持步长):{{ l|slice:'0:4:2' }}</p>

6.切取字符:

  <p>切取字符(包括...三个点):{{ info|truncatechars:6 }}</p>

7.切取单词:

  <p>切取单词(按照空格切,...不算):{{ eng|truncatewords:6 }}</p>

8.移除特定的字符:

  <p>移除特定的字符:{{ info1|cut:' ' }}</p>

9.拼接操作:

  <p>拼接操作:{{ l|join:'$' }}</p>

  <p>拼接操作(加法int):{{ n|add:10 }}</p>

  <p>拼接操作(加法str):{{ s|add:info }}</p>

10.转义:

  <p>转义:{{ sss|safe }}</p>

例如:

 结果如图:

 

# 转义(补充):

在前端 :|safe

在后端:

from django.utils.safestring import mark_safe
res = mark_safe('<h1>新新</h1>')

"""
以后你在全栈项目的时候 前端代码不一定非要在前端页面书写
也可以现在先在后端写好 然后传递给前端页面
"""

标签

标签:就是在模板里面使用流程控制,比如:if else elseif for循环等

支持一些基本运算法:and or > >= <= != in not in is is not

标签语法:{% tag %}

for标签:

# html中:
<body>
{#for标签#}
{% for name in lst %}
    <p>
        {{ name }} # 每次取一个元素,循环取完
    </p>
{% endfor %}
</body>

# views中:
def home(request):
    lst = ['chen','lin','hua','cai']
    return render(request,'home.html',locals())

前端html:

 后端views:

结果:

 {forloop}:

{% for foo in l %}

  <p>{{ forloop }}</p>

{% endfor %}

结果:

 

forloop.counter  The current iteration of the loop (1-indexed) 当前循环的索引值(从1开始)
forloop.counter0  The current iteration of the loop (0-indexed) 当前循环的索引值(从0开始)
forloop.revcounter  The number of iterations from the end of the loop (1-indexed) 当前循环的倒序索引值(从1开始)
forloop.revcounter0  The number of iterations from the end of the loop (0-indexed) 当前循环的倒序索引值(从0开始)
forloop.first  True if this is the first time through the loop 当前循环是不是第一次循环(布尔值)
forloop.last  True if this is the last time through the loop 当前循环是不是最后一次循环(布尔值)
forloop.parentloop  本层循环的外层循环(了解)

遍历字典

#views中:
def home(request):
    dd = {'username': "jerry", 'age': 18, "gender": "male"}
    return render(request,'home.html',locals())

# html中:
{% for key,val in dd.items %}
    <p>{{ key }}:{{ val }}</p>
{% endfor %}

{% for foo in dd.keys %}
    <p>{{ foo }}</p>
{% endfor %}

{% for value in dd.values %}
    <p>{{ value }}</p>

 

结果:

 

if标签:

#views中:
def home(request):
    # a = 1# 当只有a或只有还有其他值时,匹配到a,页面就显示a对应的内容
    # b = 2
    # c = 3 # 当有c值,或什么值都没有时就会执行html中的else
    return render(request, 'home.html', locals())

#html:
{% if a %}
    <p>这个是a</p>
{% elif b %}
    <p>这里是b</p>
{% else %}
    <p>这里是c</p>

结果:

if语句支持 and 、or、==><!=<=>=、in、not in、is、is not判断。

 with起别名:

{% with d.hobby.3.info as nb  %}
    <p>{{ nb }}</p>
    # 在with语法内就可以通过as后面的别名快速的使用到前面非常复杂获取数据的方式
    <p>{{ d.hobby.3.info }}</p>
{% endwith %}

结果:

 模板的继承

继承:

  一个页面继承另外一个页面,继承之后的页面和被继承的页面一模一样

{% extends 'home.html' %} # {% extends '继承的html页面' %}

继承了之后,需要自己提前规定需要修改的区域。

划定修改区域:

{% block content %}
模版内容(要被修改的内容)
{% endblock %}

子页面就可以声明想要修改哪块划定了的区域:

{% block content %}
子页面内容(修改内容)
{% endblock %}

一个页面中可以有多个被修改的区域,只需要block进行划分就行
# 被修改的区域尽量不要过多

一般情况下模板页面上应该至少要有三块可以被修改的区域

css区域:

  {% block css %}

  {% endblock %}

html区域:

  {% block content %}

  {% endblock %}

js区域:

  {% block js %}

  {% endblock %}

模板的导入

将页面的某一个局部当成模板的形式,哪个地方需要就可以直接导入使用即可

模板的导入:{% include 'hello.html' %}

 

posted @ 2023-08-02 19:35  Maverick-Lucky  阅读(8)  评论(0)    收藏  举报