Fork me on GitHub

Django笔记(二)

Django的模板-Templates

什么是模板

  • 模板就是要动态的给用户呈现的网页内容
  • 其实就是一个网页,前后端结合的一个网页

模板的设置

  • 在settings.py中TEMPLATES变量
  • 是一个列表,里面里面装的是字典

列表设置源码

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Python

模板设置源码解析

  • 1 BACKEND 指定模板的搜索引擎,不用动
  • 2 DIRS 指定模板所存放的目录们
    • DIRS=[‘index.temp’,’music.temp’]
    • 但是,如果DIRS为空的话,nameDjango会自动到每个应用找一个叫templates的文件夹来作为模板的管理目录
  • 推荐:
    • 1 DIRS内容保持为空
    • 2 在每个应用中,都创建一个templates的目录
  • 3 APP_DIRS
    • True:首先从DIRS中指定的目录中查找模板,如果没找到的话,再搜搜templates目录

模板的加载方式

  • 常用的就就一种,一般有两种方式
    • 1 使用loader获取模板,并通过HttpResponse进行响应
    • 2 使用render直接加载模板并返回
    • from django.shortcuts import render

模板的语法

  • 变量
    • 1 作用,将后端数据传递给模板,在模板中,会根据变量的值进行显示
    • 2 在Django中允许传递给模板做变量的数据类型,数字,字符串,列表,元组,字典,函数,对象
    • 3 变量的语法
loader
dic={
    '变量名1':'值1',
    '变量名2':'值2',
}
  • 在模板中显示变量
    • 字典 字典:{{d.name}}

练习

  • 当方位news/exer时,在模板中显示以下内容
  • 书名:<<背影>>
  • 作者:朱自清
  • 出版社:北京大学出版社
  • 出版时间: 1995-10-12

参考答案

{%extends 'public/base.html'%}
{%block title%}
变量传递
{%endblock%}
{%block content%}
<table class="table table-bordered table-hover  table-striped">
    <thead>
    <tr>
        <th>属性</th>
        <th>值</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>书名</td>
        <td>{{d.书名}}</td>
    </tr>
    <tr>
        <td>作者</td>
        <td>{{d.作者}}</td>
    </tr>
    <tr>
        <td>出版社</td>
        <td>{{d.出版社}}</td>
    </tr>
    <tr>
        <td>出版时间</td>
        <td>{{d.出版时间}}</td>
    </tr>
    </tbody>
</table>
{%endblock%}
HTML

标签

  • 什么是标签
    • 允许将服务端的一些功能嵌入到模板中
  • 语法
    • {%标签内容%} 部分标签有结束标签
  • 注释
  • 循环

     

{% for x, y in points %}
    There is a point at {{ x }},{{ y }}
{% endfor %}

forloop.counter 从1开始记录迭代次数
Python
  • 用empty代替if去判断如果数组为空
<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Python
  • if语句
{% if var1 %}
    {{ var1 }}
{% elif var2 %}
    {{ var2 }}
{% elif var3 %}
    {{ var3 }}
{% endif %}
Python

列表嵌套字典实战案例源码

前端源码

{%extends 'public/base.html'%}
{%block title%}
变量传递
{%endblock%}
{%block content%}
<table class="table table-bordered table-hover  table-striped">
    <thead>
    <tr>
        <th>#</th>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    </thead>
    <tbody>
    {% for i in l %}
    <tr>
        <td>{{forloop.counter}}</td>
        <td>{{i.name}}</td>
        <td>{{i.age}}</td>
    </tr>
    {% endfor %}
    </tbody>
</table>
{%endblock%}
HTML

后台源码

# 表格
def table(request):
    l = []
    for i in range(100):
        d = {'name': 'lxgzhw' + str(i), 'age': i}
        l.append(d)
    return render(request, 'table.html', {
        'l': l,
    })

Python

常用标签

  • 1 for
    • {%for i in 列表|元组|字典%}{%endfor%}
    • forloop.counter记录循环次数
    • forloop.frst 是否为第一个 last 是否为最后
  • 2 if
    • {%if 条件%}{%endif%}
    • 条件 比较运算符 逻辑运算符
  • 3 文本内容截取
    • {{ some_list|slice:":2" }} 切片切取指定长度的文本
    • {{ value|truncatechars:9 }}
    • 如果字符串长于指定的字符数,则截断该字符串。截断的字符串将以可翻译的省略号序列(“…”)结束。
  • 4 显示带行号的文本
    • {{ value|linenumbers }}
  • 5 将纯文本的中\n换行符转换为html的br换行符

    • {{ value|linebreaksbr }}
  • 6 返回字符串或列表的长度
    • {{ value|length }}
  • 7 使用字符串拼接列表
    • {{ value|join:" // " }}
  • 8 将文件大小格式化
    • 格式,如一个“人类可读”的文件大小的值(即, ,等等)。'13 KB'``'4.1 MB'``'102 bytes'
    • {{ value|filesizeformat }}
posted @ 2019-03-11 11:02  seymourgao  阅读(129)  评论(0编辑  收藏  举报