'''
就是在模板里面使流程控制:if else elseif for
标签看起来像是这样的: {% tag %}
'''
1.for标签
{% for person in person_list %}
<p>{{ person.name }}</p>
{% endfor %}
注:循环序号可以通过{{forloop}}显示
forloop.counter The current iteration of the loop (1-indexed) 当前循环的索引值(从1开始)
forloop.counter0 The current iteration of the loop (0-indexed) 当前循环的索引值(从0开始)
forloop.revcounter The number of iterations from the end of the loop (1-indexed) 当前循环的倒序索引值(从1开始)
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed) 当前循环的倒序索引值(从0开始)
forloop.first True if this is the first time through the loop 当前循环是不是第一次循环(布尔值)
forloop.last True if this is the last time through the loop 当前循环是不是最后一次循环(布尔值)
forloop.parentloop 本层循环的外层循环
2.遍历字典
{% for key,val in dic.items %}
<p>{{ key }}:{{ val }}</p>
{% endfor %}
{% for foo in d.keys %}
<p>{{ foo }}</p>
{% endfor %}
{% for foo in d.values %}
<p>{{ foo }}</p>
{% endfor %}
3.if 标签
{% if num > 100 or num < 0 %}
<p>无效</p>
{% elif num > 80 and num < 100 %}
<p>优秀</p>
{% else %}
<p>凑活吧</p>
{% endif %}
if语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。
4.with起别名
d = {'username':'kevin','age':18,'info':'这个人有点意思','hobby':[111,222,333,{'info':'NB'}]}
{% with d.hobby.3.info as nb %}
<p>{{ nb }}</p>
在with语法内就可以通过as后面的别名快速的使用到前面非常复杂获取数据的方式
<p>{{ d.hobby.3.info }}</p>
{% endwith %}
5.csrf_token
<form action="" method="post">
{% csrf_token %}
<input type="submit">
</form>
# 1.年龄大于35岁的数据
res = models.UserInfo.objects.filter(age__gt=35).all()
print(res)
# 2.年龄大于等于20岁的数据 e---------->equal
res = models.UserInfo.objects.filter(age__gte=20).all()
print(res)
# 3. 年龄是10 或者 20 或者 30
res = models.UserInfo.objects.filter(age__in=[10, 20, 300]).all()
print(res)
# 4.年龄在18到40岁之间的 首尾都要
# from table where age between 18 and 40
res = models.UserInfo.objects.filter(age__range=[18, 40])
print(res)
# 5. 查询出名字里面含有s的数据 模糊查询
# from table where username like '%s%'
res = models.UserInfo.objects.filter(username__contains='s').all()
print(res)
# 6.用户名以s开头的
res = models.UserInfo.objects.filter(username__startswith='s').all() # like 's%'
# res = models.UserInfo.objects.filter(username__endswith='s').all() # like '%s'
print(res)
# 7.查询出注册时间是 2023 5月
res = models.UserInfo.objects.filter(reg_time__month=5, reg_time__year=2023, reg_time__day=1).all()
res = models.UserInfo.objects.filter(reg_time__month=5, reg_time__day=1).all()
print(res)