模板的基本使用及其继承

在Django框架中,模板是可以帮助开发者快速生成呈现给用户页面的工具 。  
模板的设计方式实现了我们MVT重VT的解耦,VT有着N:M的关系,一个V可以调用任意T,一个T可以供任意V使用。 模板处理分为两个过程: 加载和渲染。

创建模板

创建与应用和项目同级的模板文件夹,并修改项目中的配置文件使其生效。

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',
            ],
        },
    },
]

'DIRS': [os.path.join(BASE_DIR,'templates')],模板所在目录

修改配置文件

模板语法及相关例子

注释

注释可见,可运行
<!-- 注释内容 -->

单行注释注释不可见,不可运行

单行注释(页面源码中不会显示注释内容)

{# 被注释掉的内容 #}

多行注释注释不可见,不可运行
{% comment %}

{% endcomment %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <!--<link rel="stylesheet" href="/static/css/index.css">-->
    <link rel="stylesheet" href="{% static 'css/index.css' %}">

</head>
<body>

<h2>你好</h2>

<table>
    <thead>
    <th>序号</th>
    <th>id</th>
    <th>name</th>
    <th>age</th>
    </thead>
    <tbody>
    {% for stu in students %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ stu.id}}</td>
        {# <td {% if stu.id == 3 %} style="color:red;"{% endif %}>{{ stu.s_name}}</td> #}
        <td {% if forloop.first %} style="color:red;"{% endif %}>{{ stu.s_name}}</td>
        <td {% if forloop.last %} style="color:yellow;"{% endif %}>{{ stu.s_age}}</td>
    </tr>
    {% endfor %}
    </tbody>

</table>

<!--&lt;!&ndash;解析变量&ndash;&gt;-->
<!--{% for stu in students %}-->
<!--<table>{{ stu.s_name }}</table>-->
<!--{% endfor %}-->
</body>
</html>
运行结果

模板继承

挖坑

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
        <title>
            {% block title %}
            {% endblock %}
        </title>
        {% block extCss %}
        {% endblock %}
        {% block extJs %}
        {% endblock %}
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>

填坑


{% extends 'base.html' %}

{% block title %}
    注册
{% endblock%}

{% block content %}
    <form action="" method="post">
        {{ form.errors.username }}
        <p>用 户 名:<input type="text" name="username"></p>
        {{ form.errors.password }}
        <p>密    码:<input type="password" name="password"></p>
        {{ form.errors.password2 }}
        <p>确认密码:<input type="password" name="password2"></p>
        <input type="submit" value="提交">
    </form>
{% endblock%}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 这个不错分享给大家,从扣上看到的,就转过来了 《电脑专业英语》 file [fail] n. 文件;v. 保存文...
    麦子先生R阅读 6,685评论 5 24
  • 白子画可以不被人理解,却不应该被肆意地伤害,沾染尘埃。在白子画的世界里,爱情太小,他是九天之上的仙人,天地众生...
    燕语玑珠阅读 419评论 0 0
  • 首先,这个课是我们点系列课程的第一节课,首先,让孩子们明白点是独立的个体,让孩子在错乱中通过观察和实验形成的有规律...
    82b35b1ee1d5阅读 249评论 0 0
  • 2018年5月15日下午3点半我们如约而至,参加了魏换霞老师带领的内部成长活动。 活动中,段老师...
    徐玉霞阅读 442评论 0 0