django后端学习B站网课:点击跳转
或在浏览器粘贴网址:https://www.bilibili.com/video/BV1vK4y1o7jH?p=10&vd_source=597e21cf34ffcdce468ba00be2177e8a
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 '汪汪'
<!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>
path('test_html_param',views.test_html_param)
将一些服务器端的功能嵌入到模板中,例如流程控制等
{% 标签 %}
…
{% 结束标签 %}
{% if 条件表达式1 %}
…
{% elif 条件表达式2 %}
…
{% elif 条件表达式3 %}
…
{% else %}
…
{% endif %}
def test_if_for(request):
dic={}
dic['x'] = 10
return render(request,'test_if_for.html',dic)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试if和for</title>
</head>
<body>
{% if x > 10 %}
今天天气很好
{% else %}
今天天气非常好
{% endif %}
</body>
</html>
path('test_if_for',views.test_if_for)
<!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 变量 in 可迭代对象 %}
…循环语句
{% empty %}
…可迭代对象无数据时填充的语句
{% endfor %}
变量 | 描述 |
---|---|
forloop.counter | 循环的当前迭代(从1开始索引) |
forloop.counter() | 循环的当前迭代(从0开始索引) |
forloop.revcounter | counter值得倒序 |
forloop.revcounter() | revcounter值的倒序 |
forloop.first | 如果这是第一次通过循环,则为真 |
forloop.last | 如果这是最后一次通过循环,则为真 |
forloop.parentloop | 当嵌套循环,parentloop表示外层循环 |
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']
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试if和for</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 %}如果是最后一次循环,则输出=====