Django后端开发——模板层-变量和标签

本文介绍了Django模板中的变量使用、if和for标签的语法、作用以及实战示例。包括如何在views.py中定义变量,以及在templates文件夹下的test_html_param.html和test_if_for.html中展示这些变量和进行条件判断。同时,讨论了模板层中标签的使用以及在练习中遇到的问题和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


参考资料

django后端学习B站网课:点击跳转
或在浏览器粘贴网址:https://www.bilibili.com/video/BV1vK4y1o7jH?p=10&vd_source=597e21cf34ffcdce468ba00be2177e8a


一、模板的变量

在这里插入图片描述


在这里插入图片描述

views.py

def test_html_param(request):
    dic={}
    dic['int']=88
    dic['str']='hahaha'
    dic['lst']=['Tom','Jack','Lily']
    dic['dict']={'a':9,'b':8}
    dic['func']=say_hi
    dic['class_obj']=Dog()

    return render(request,'test_html_param.html',dic)

def say_hi():
    return 'hihihi'

class Dog:
    def say(self):
        return '汪汪'

test_html_param.html(在templates文件夹下新建)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>int{{ int }}</h3>
<h3>str is {{ str }}</h3>
<h3>lst is {{ lst }}</h3>
<h3>lst is {{ lst.0 }}</h3>
<h3>dict is {{ dict }}</h3>
<h3>function is {{ func }}</h3>
<h3>class_obj is {{ class_obj.say }}</h3>
</body>
</html>

urls.py

path('test_html_param',views.test_html_param)

效果

在这里插入图片描述

二、模板层的标签

模板标签的作用

将一些服务器端的功能嵌入到模板中,例如流程控制等

标签语法

{% 标签 %}

{% 结束标签 %}

if 标签

语法

{% if 条件表达式1 %}

{% elif 条件表达式2 %}

{% elif 条件表达式3 %}

{% else %}

{% endif %}

注意

  1. if条件表达式里可以用的运算符==,!=,<, >, <=, >=, in, not in, is, is not, not、and、or
  2. 在if标记中使用实际括号是无效的语法。如果需要它们指示优先级,则应该用嵌套的if标记

示例

views.py
def test_if_for(request):
    dic={}
    dic['x'] = 10
    return render(request,'test_if_for.html',dic)
test_if_for.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试iffor</title>
</head>
<body>

{% if x > 10 %}
今天天气很好
{% else %}
今天天气非常好
{% endif %}

</body>
</html>
urls.py
path('test_if_for',views.test_if_for)
效果

在这里插入图片描述

练习

在这里插入图片描述

test_mycal 代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/test_mycal" method="post">
        <input type="text" name="x" value="{{ x }}">
        <select name="op">
            <option value="add" >+</option>
            <option value="sub" >-</option>
            <option value="mul" >*</option>
            <option value="div" >/</option>
        </select>
        <input type="text" name="y" value="{{ y }}">=<span>{{ result }}</span>
        <div>
            <input type="submit" value="开始计算">
        </div>
    </form>

</body>
</html>
遇到的问题

在视频里老师提到如果不加if标签,计算结束后会op会恢复默认值add(经过我实践操作确实是这样的)
但是不知道为什么加上if标签之后会报错,看弹幕有人说删去空格之类的,但是对于我都没效果
更奇怪的就是我再删去if标签之后,op就不会恢复默认值了…
这个问题我目前还没有解决,如果后面解决了会分享给大家的~

在这里插入图片描述

for标签

语法

{% for 变量 in 可迭代对象 %}
…循环语句
{% empty %}
…可迭代对象无数据时填充的语句
{% endfor %}

内置变量 forloop

变量描述
forloop.counter循环的当前迭代(从1开始索引)
forloop.counter()循环的当前迭代(从0开始索引)
forloop.revcountercounter值得倒序
forloop.revcounter()revcounter值的倒序
forloop.first如果这是第一次通过循环,则为真
forloop.last如果这是最后一次通过循环,则为真
forloop.parentloop当嵌套循环,parentloop表示外层循环

示例

views.py
def test_if_for(request):
    dic={}
    dic['x'] = 10
    dic['lst'] = ['Tom','Jack','Lily']
    return render(request,'test_if_for.html',dic)

添加内容:

dic['lst'] = ['Tom','Jack','Lily']
test_if_for.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试iffor</title>
</head>
<body>

{% if x > 10 %}
今天天气很好
{% else %}
今天天气非常好
{% endif %}


<br>
{% for name in lst %}
    {% if forloop.first %} &&&&& {% endif %}
<p>{{ forloop.counter }}{{ name }}</p>
    {% if forloop.last %} ===== {% endif %}
{% empty %}
    当前没数据
{% endfor %}

</body>
</html>

添加内容:

<br>
{% for name in lst %}
    {% if forloop.first %} &&&&& {% endif %}
<p>{{ forloop.counter }}{{ name }}</p>
    {% if forloop.last %} ===== {% endif %}
{% empty %}
    当前没数据
{% endfor %}

br、p控制换行
{{ forloop.counter }}加序号
{% if forloop.first %} &&&&& {% endif %}如果是第一次循环,则输出&&&&&
{% if forloop.last %} ===== {% endif %}如果是最后一次循环,则输出=====

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

^_^2412

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值