Django-06 模板层变量 if标签、for标签 模板标签 1, if标签 2,for标签

能传递到模板中的数据类型

  • str - 字符串
  • int - 整型
  • list - 数组
  • tuple - 元组
  • dict - 字典
  • func - 方法
  • obj - 类实例化的对象

在模板中使用变量语法

  • {{ 变量名 }}
  • {{ 变量名.index }}
  • {{ 变量名.key }}
  • {{ 对象.方法 }}
  • {{ 函数名 }}

例子

views.py

def test_html(request):
    from django.shortcuts import render
    dic = {}
    dic['int'] = 88
    dic['str'] = 'wanger'
    dic['lst'] = ['Tom','Jack','Lily']
    dic['func'] = say_hi
    dic['class_obj'] = Dog()
    return render(request,'test_html.html',dic)

def say_hi():
    return 'hahaha'

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

test_html.html

<body>
    <h3>int 是 {{ int }}</h3>
    <h3>str 是 {{ str }}</h3>
    <h3>lst 是 {{ lst }}</h3>
    <h3>lst.0 是 {{ lst.0 }}</h3>
    <h3>dict 是 {{ dict }}</h3>
    <h3>dict['a'] 是 {{ dic.a }}</h3>
    <h3>function 是 {{ func }}</h3>
    <h3>class_obj 是 {{ class_obj.say }}</h3>
</body>

模板标签

作用:将一些服务器端的功能嵌入到模板中,例如流程控制等
标签语法

{% 标签 %}
...
{% 结束标签 %}

1, if标签

{% if 条件表达式1 %}
...
{% elif 条件表达式2 %}
...
{% elif 条件表达式3 %}
...
{% else %}
...
{% endif %}

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

djanggo中 if 的使用必须是:

{%if ♦ 变量名 ♦ 判断符 ♦ 条件%}

其中♦ 表示空格,否则会报错:

Could not parse the remainder: ‘%‘ from ‘%‘ 以及 Could not parse the remainder: ‘==“addd“‘ from ‘==“ad

小练习:计算器

mycal.html

<form action="/mycal" method="post">
        <input type="text" name="x" value="{{ x }}">
        <select name="op">
            <option value="add" {% if op == 'add' %} selected{%endif%}>+</option>
            <option value="sub" {% if op == 'sub' %} selected{%endif%}>-</option>
            <option value="mul" {% if op == 'mul' %} selected{%endif%}>*</option>
            <option value="div" {% if op == 'div' %} selected{%endif%}>/</option>
        </select>
        <input type="text" name="y" value="{{ y }}">=
        <span>{{ result }}</span>
        <div><input type="submit" value='开始计算'></div>
    </form>

views.py

def test_mycal(request):
    if request.method == 'GET':
        return render(request,'mycal.html')
    elif request.method == 'POST':
        x = int(request.POST['x'])
        y = int(request.POST['y'])
        op = request.POST['op']
        result = 0
        if op == 'add':
            result = x + y
        elif op == 'sub':
            result = x - y
        elif op == 'mul':
            result = x * y
        elif op == 'div':
            result = x / y
        return render(request,'mycal.html',locals())

2,for标签

语法

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

内置变量-forloop

变量 描述
forloop.counter 循环的当前迭代(从1开始索引)
forloop.counter() 循环的当前迭代(从0开始索引)
forloop.revcounter counter值的倒序
forloop.revcounter() counter()值的倒序
forloop.first 如果这是第一次通过循环,则为真
forloop.last 如果这是最后一次循环,则为真
forloop.parentloop 当嵌套循环,parentloop表示外层循环
    {% for name in lst %}
        {% if forloop.first %}$$${%endif%}
        <p>{{ forloop.counter }} {{name}} </p>
        {% if forloop.last %}---{%endif%}
    {% empty %}
    当前没数据
    {% endfor %}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章