Django学习5:模板介绍及模板标签的使用

目录

一、模板介绍

二、模板渲染的常用方式:

方式一、render_to_string渲染

方式二、使用render(request,'index.html') 的方式渲染

三、模板的路径及查找顺序

四、DTL模板语法

1.模板变量

2.常用的模板标签

1. if标签:

 2.for..in标签

3.with标签

4.verbatim标签


一、模板介绍

视图函数只是返回文本,而实际上网页大多是有样式和HTML代码的,模板就是用来渲染、显示视图的,相当于HTML的预编译器

市面上有的模板系统,最知名的就是DTL和Jinja2,DTL是和Python无缝对接的框架

注:在Pycharm环境下,DTL的模型HTML文件中:

1.所有的标签输入标签名,点击Tab键,就会自动出来格式,比如输入if,自动就会出来

{% if %}

{% endif  %}

2.按Ctrl+/就可以注释掉代码

二、模板渲染的常用方式:

方式一、render_to_string渲染

1.新建的项目中的urls.py做url和函数视图的映射:

from django.contrib import admin
from django.urls import path
from front import views
urlpatterns = [
    #path('admin/', admin.site.urls),
    path('', views.index),
]

2. #新建的应用中的views.py

from django.template.loader import render_to_string
from django.http import  HttpResponse
# Create your views here.

def index(request):
    html = render_to_string("index.html")
    return HttpResponse(html)

3. #在templates文件中新建一个index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是从模板渲染的字符串
</body>
</html>

方式二、使用render(request,'index.html') 的方式渲染

from django.shortcuts import render

def index(request):
    #html = render_to_string("index.html")
    #return HttpResponse(html)
    return render(request,'index.html')

 

三、模板的路径及查找顺序

1.默认DIR为他首先寻找的路径,如果DIR为空,则寻找APP中的模板

这是setting.py里的模板路径设置

2.要想使用APP中的模板,需要满足

  • 在该APP中要有templates文件
  • 该APP在setting.py里边的INSTALL_APP里已经安装
  • APP_DIRS设置为TRUE

3.当在DIRS默认的项目模板和自己应用的模板都找不到模板文件时,就会去其他应用找模板文件

 

 

四、DTL模板语法

  • 1.模板变量

context参数是一个字典,对应的键值对中,值可以是字符,如下:

from django.shortcuts import render

def index(request):
    context = {
        'song_name':'someone like you'
    }
    return render(request, 'index.html', context=context)
#templates里的index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 {{ song_name }}
</body>
</html>

 对应的值也可以是对象,在HTML里需要用“ . ”连接,如下

 对应的值也可以是一个字典,如下:

song_name.

对应的值也可以是一个列表、元组,如下

song_name.0

 

 

  • 2.常用的模板标签

1. if标签:

if标签的判断运算和Python中是一样的,“==,>,<,!=,,,,”

 如:在views.py里定义了一个年龄的参数,通过渲染传给模板让其判断是否成年

def index(request):
    context = {
        'age': 18
    }
    return render(request, 'index.html', context=context)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 {% if age < 18 %}
     <p>您是未成年人</p>
  {% elif age == 18 %}
     <p>您刚满18岁</p>
 {% else %}
     <p>您已经成年</p>
 {% endif %}
</body>
</html>

 2.for..in标签

  • 1.列表的遍历:
from django.shortcuts import render
# Create your views here.

def index(request):
    context = {
        'songs':[
            "Rolling in the deep",
            "Someone like you",
            "Set fire to the rain",
        ]
    }
    return render(request, 'index.html', context=context)

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <ul>
        {% for song in songs %}
            <li>{{ song }}</li>
        {% endfor %}

    </ul>

</body>
</html>

  • 2.字典的遍历

 在veiws.py的context中,加一个songdetail的字典

    context = {
        'songs':[
          'Rolling in the deep',
          'someone like you',
          'set fire to the rain',
        ],
        'songdetail':{
            'songname': 'Rolling in the deep',
            'singer': 'Adele',
            'album':'21'
        }
    }

在模板的index.html中分别遍历键key和元组item 

    <ul>
        {% for key in songdetail.keys %}
            <li>{{ key }}</li>
        {% endfor %}

    </ul>

    <ul>
        {% for key,value in songdetail.items %}
            <li>{{ key }}/{{ value }}</li>
        {% endfor %}
    
    </ul>

  • 3.表格:列表中元组的遍历——table的使用:

在views.py中的def函数的列表里定义了字典

def index(request):
    context = {
        'songs': [
            {
                'songname':'Rolling in the deep',
                'singer':'adele',
                'album':'21',
            },
            {
                'songname': 'Hello',
                'singer': 'adele',
                'album': '25',
            },
            {
                'songname': 'One and Only',
                'singer': 'adele',
                'album': '19',
            }
        ]
    }

 在项目的模板的index.html中,利用表格遍历,thead是对表头的定义,tbody是对表格内容的定义,forloop.counter0是显示列表中第几个元组的序号,且是从0开始

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <table>
        <thead>
            <tr>
                <td>序号</td>
                <td>歌名</td>
                <td>歌手</td>
                <td>专辑</td>
            </tr>
        </thead>
        <tbody>
            {% for song in songs %}
                <tr>
                    <td>{{ forloop.counter0 }}</td>
                    <td>{{ song.songname }}</td>
                    <td>{{ song.singer }}</td>
                    <td>{{ song.album }}</td>
                </tr>
            {% endfor %}

        </tbody>

    </table>

</body>
</html>

forloop.counter的其他使用方法:

 

3.with标签

用途:假如在显示列表内容的时候,需要用“列表名.序号”的形式,有些冗杂,可以用with为“列表名.序号”起个别名,在使用时也直接使用别名

假设有列表persons,第一个元组即是person.0,获取其内容可以给它取个别名

'with'使用第一种方式是

            {% with persons.0 as zs  %}
                <p>zs</p>
                <p>zs</p>
            {% endwith %}
     

第二种是

        {% with zs=persons.0  %}
            <p>zs</p>
            <p>zs</p>
        {% endwith %}

4.verbatim标签

在DTL模板中,默认是会解析那些特殊字符或标签的,如{%%},但是如果不想让它解析,就可以用此标签

 

参考教程:https://www.bilibili.com/video/av93363026?p=30

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章