Django的模板系统

一、模板的组成

html代码和逻辑控制代码

二、逻辑控制代码的形式

1、变量(使用双大括号引用变量)

a、template和context

语法 :  {{ var_name }}

模板系统不仅可以传字符串,它可以传递任意对象,对于向列表、字典、元组等对象,我们可以用句点来处理,叫做深度变量的查找

eg

对应views.py

def index(request):
    l = [1, 2, 3]
    return render(request, 'index.html', locals())

 对应index.html

<body>
<h1>传递过来的列表的第一个元素是 {{ l.0 }}</h1>
</body>

浏览器返回

传递过来的列表的第一个元素是 1

b、变量的过滤器filter

语法格式 : {{ 对象|filter:参数}}

过滤器中有add、addslashes、capfirst、cut、date、default、default_if_none等

add:给变量加上相应的值

addslashes:给变量中的引号前加斜线

capfirst:首字母大写

cut:去除指定字符

date:格式化时间

。。。

如果传入的变量是一段标签,并想要实现其功能,则应给使用safe函数

eg:

不用safe函数

<body>
<h1>传递过来的列表的第一个元素是 {{ l.0 }}</h1>
这是一个{{ s }}
</body> 

浏览器输出

这是一个<a href='#'>跳转链接</a> 

使用safe函数

<body>
<h1>传递过来的列表的第一个元素是 {{ l.0 }}</h1>
这是一个{{ s|safe }}
</body>

浏览器输出

这是一个跳转链接

autoescape也可以实现同样的功能

eg

<body>
<h1>传递过来的列表的第一个元素是 {{ l.0 }}</h1>
这是一个{{ s|safe }}<br>
{% autoescape off %}
    这是一个{{ s }}
{% endautoescape %}
</body>

  2、标签(tag)的使用(使用大括号和百分比的组合来表示使用tag)

语法:{% tag %}

a、{% if %}的使用,做判断,有if就要有{% endif %}

eg:

{% if l.0 > 3 %}
    <h2>
    {{ l.0 }}
    {% else %}
        <h2>
    {{ l.2 }}
{% endif %}
</h2> 

b、{% for %}的使用,做遍历,同样有for就要有{% endfor %}

eg:

{% for num in l %}
    <p>{{ num }}</p>
{% endfor %} 

for循环中还内置了forloop模板变量,forloop.counter表示循环的次数,它从1开始计数,第一次循环设为1,forloop.counter0,从0开始计数,forloop.revcounter反转计数,forloop.first第一次循环时其值为True

eg:

{% for num in l %}
    <p>{{ forloop.counter0 }}:{{ num }}</p>
    <p>{{ forloop.counter }}:{{ num }}</p>
{% endfor %}

c、{%csrf_token%}:csrf_token标签

当用post的方法提交表单时,django会报一个错误,这是django一个保护机制,用来防止跨站攻击

eg:

<form action="/blog/register/" method="post">
    姓名<input type="text" name="username"><br>
    密码<input type="text" name="psw"><br>
    <input type="submit">
</form>

报错信息

Forbidden (403)

CSRF verification failed. Request aborted.

You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.

If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests.

添加后顺利提交

<form action="/blog/register/" method="post">
    姓名<input type="text" name="username"><br>
    密码<input type="text" name="psw"><br>
    <input type="submit">
    {% csrf_token %}
</form>

 d、{% url %} 引用路由配置的地址

eg:

<form action="{% url 'reg' %}" method="post">
    姓名<input type="text" name="username"><br>
    密码<input type="text" name="psw"><br>
    <input type="submit">
    {% csrf_token %}
</form>

req为路由配置中的别名 

e、{% verbatim %}: 禁止render,有开始就有结束{% endverbatim %}

包含在其中的代码将不被render渲染

f、{% load %}: 加载标签库

g、{% with %}:用更简单的变量名替代复杂的变量名,需要{% endwith %}

eg:

{% with s=qwer %}
    {{ s }}
{% endwith %}

注意:s=qwer之间不得有空格,否则报错


 3、自定义filter和simple_tag

a、在app中创建templatetags模块(必须的)

b、创建任意 .py 文件,如:mytag.py

eg:

自定义

from django import template
from django.utils.safestring import mark_safe


register = template.Library()#变量名必须时register


@register.filter
def filter_mul(a, b):
    return a*b


@register.simple_tag()
def tag_mul(a, b):
    return a*b

如何使用

在html文件的第一行加载{% load mytag %}即可,这里创建的文件为mytag.py

eg:

{% load mytag %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BBU</title>
</head>

需要注意的是   filter可以用在if等语句后,simple_tag不可以

猜你喜欢

转载自www.cnblogs.com/sumcet/p/8996723.html