Django(二) 模板:基本使用、模板语法、模板继承

一、模板的使用实战

接:https://blog.csdn.net/u010132177/article/details/103788677
参考:https://docs.djangoproject.com/zh-hans/3.0/contents/
https://docs.djangoproject.com/zh-hans/3.0/intro/overview/#write-your-views

1)创建模板文件夹templates

  • 在项目根目录下创建【/templates/app1/index.html】文件及目录
    /templates/index.html 文件代码:
<!--花括号内是变量名,对应views.py中的字典键名-->
<h1>{{ hello }}----{{wa}}</h1>

从模板中我们知道变量使用了双括号。

2)在settings.py中配置模板路径

  • 接下来我们需要向Django说明模板文件的路径,修改project1/settings.py,修改 TEMPLATES 中的 DIRS 为 [BASE_DIR+"/templates",][os.path.join(BASE_DIR,'templates')],,如下所示:

/project1/settings.py 文件代码:

...TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates",],    # 修改位置 或[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',
            ],
        },
    },
]
...

注:BASE_DIR来自前面定义,即当前项目的绝对路径:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

3)在视图中渲染模板

  • 我们现在修改 view.py,增加一个新的对象,用于向模板提交数据
    【1】定义1个字典
    【2】向字典写一个键:值(hello:'hello world!!')
    【3】返回:把context渲染到app1/index.html的模板文件
    /project1/view.py 文件代码:
from django.shortcuts import render #渲染模块
#from django.http import HttpResponse
# Create your views here.

def index(request): # request为必备参数
    context={} #【1】定义1个字典
    context['hello']='hello world!!!' #【2】向字典写一个键:值(hello:'hello world!!')
    context['wa']='wawawawawahahahaha!'
    return render(request,'app1/index.html',context) #【3】返回:把context渲染到app1/index.html的模板文件
    #return HttpResponse('hello python!')
  • 可以看到,我们这里使用 render 来替代之前使用的 HttpResponse。render 还使用了一个字典 context 作为参数。
  • context 字典中元素的键值 "hello" 对应了模板中的变量 "{{ hello }}"。

效果:再访问访问 http://127.0.0.1:8000/app1:

hello world!----wawawawawahahahaha!

  • 这样我们就完成了使用模板来输出数据,从而实现数据与视图分离。

二、模板中常用的语法规则

2.0先看一个在模板循环列表的实例

1)app1/views.py

【2.2】定义一个字典值为一个列表,list为把内容转换为列表

from django.shortcuts import render

def index(request):
    context={} #【1】定义1个字典
    context['hello']='hello world!!!' #【2】向字典写一个键:值(hello:'hello world!!')
    context['wa']='wawawawawahahahaha!'
    context['list']=list(range(1,10)) #【2.2】定义一个字典值为一个列表,list为把内容转换为列表
    return render(request,'app1/index.html',context) #【3】返回:把context渲染到app1/index.html的模板文件

2)templates/app1/index.html

重点:【循环出列表】

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>app1模板首页</title>
</head>
<body>
<!--括号内是变量名,对应views.py中的字典键名-->
<h1 style="color:red">{{ hello }}----{{wa}}</h1>
<br/>
【循环出列表】:{{list}}
<ul>
    {% for i in list %}
    <li>{{i}}</li>
    {% endfor %}
</ul>

</body>
</html>

3)效果

在这里插入图片描述

2.1 if/else 标签

基本语法格式如下:

{% if condition %}
     ... display
{% endif %}

# 或者:

{% if condition1 %}
   ... display 1
{% elif condition2 %}
   ... display 2
{% else %}
   ... display 3
{% endif %}

根据条件判断是否输出。if/else 支持嵌套。

{% if %} 标签接受 and , or 或者 not 关键字来对多个变量做判断 ,或者对变量取反( not ),例如:

{% if athlete_list and coach_list %}
     athletes 和 coaches 变量都是可用的。
{% endif %}

2.2 for 标签

{% for %} 允许我们在一个序列上迭代。

与Python的 for 语句的情形类似,循环语法是 for X in Y ,Y是要迭代的序列而X是在每一个特定的循环中使用的变量名称。
每一次循环中,模板系统会渲染在 {% for %} 和 {% endfor %} 之间的所有内容。
例如,给定一个运动员列表 athlete_list 变量,我们可以使用下面的代码来显示这个列表:

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% endfor %}
</ul>

给标签增加一个 reversed 使得该列表被反向迭代:

{% for athlete in athlete_list reversed %}
...
{% endfor %}

可以嵌套使用 {% for %} 标签:

{% for athlete in athlete_list %}
    <h1>{{ athlete.name }}</h1>
    <ul>
    {% for sport in athlete.sports_played %}
        <li>{{ sport }}</li>
    {% endfor %}
    </ul>
{% endfor %}

2.3 ifequal/ifnotequal 标签

{% ifequal %} 标签比较两个值,当他们相等时,显示在 {% ifequal %} 和 {% endifequal %} 之中所有的值。

下面的例子比较两个模板变量 user 和 currentuser :

{% ifequal user currentuser %}
    <h1>Welcome!</h1>
{% endifequal %}

和 {% if %} 类似, {% ifequal %} 支持可选的 {% else%} 标签:8

{% ifequal section 'sitenews' %}
    <h1>Site News</h1>
{% else %}
    <h1>No News Here</h1>
{% endifequal %}

2.4 注释标签

Django 注释使用 {# #}。

{# 这是一个注释 #}

2.5 过滤器

模板过滤器可以在变量被显示前修改它,过滤器使用管道字符,如下所示:

{{ name|lower }}

{{ name }} 变量被过滤器 lower 处理后,文档大写转换文本为小写。

过滤管道可以被* 套接* ,既是说,一个过滤器管道的输出又可以作为下一个管道的输入:

{{ my_list|first|upper }}

以上实例将第一个元素并将其转化为大写。

有些过滤器有参数。 过滤器的参数跟随冒号之后并且总是以双引号包含。

例如:

{{ bio|truncatewords:"30" }}

这个将显示变量 bio 的前30个词。

2.6 其他过滤器:

  • addslashes : 添加反斜杠到任何反斜杠、单引号或者双引号前面。
  • date : 按指定的格式字符串参数格式化 date 或者 datetime 对象,实例:
{{ pub_date|date:"F j, Y" }}
  • length : 返回变量的长度。

2.7 include 标签

{% include %} 标签允许在模板中包含其它的模板的内容。
下面这个例子都包含了 nav.html 模板:

{% include "nav.html" %}

三、模板继承

3.1 模板可以用继承的方式来实现复用。

接下来我们先创建之前项目的 templates 目录中添加 base.html 文件,代码如下:
HelloWorld/templates/base.html 文件代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
    
    <p>菜鸟教程 Django 测试。</p>
    {% block mainbody %}
       <p>original</p>
    {% endblock %}
</body>
</html>

以上代码中,名为 mainbody 的 block 标签是可以被继承者们替换掉的部分。
所有的 {% block %} 标签告诉模板引擎,子模板可以重载这些部分。

hello.html 中继承 base.html,并替换特定 block,hello.html 修改后的代码如下:

HelloWorld/templates/hello.html 文件代码:

{%extends "base.html" %}
 
{% block mainbody %}
<p>继承了 base.html 文件</p>
<p>{{hello}}</p>
{% endblock %}

第一行代码说明 hello.html 继承了 base.html 文件。可以看到,这里相同名字的 block 标签用以替换 base.html 的相应 block。

重新访问地址 http://127.0.0.1:8000/hello,输出结果如下:

菜鸟教程 Django 测试。

继承了 base.html 文件

Hello World!
posted @ 2020-01-10 16:10  晨光曦微  阅读(499)  评论(0编辑  收藏  举报