模板之标签
# 就是在模板里面使流程控制:if else elseif for
标签看起来像是这样的: {% tag %}
for标签
{% for person in person_list %}
<p>{{ person.name }}</p>
{% endfor %}
orloop.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:本层循环的外层循环
# 遍历字典
{% 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 %}
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判断。
# 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 %}
模板的继承(重要)
# 继承:继承之后的页面跟被继承的页面一模一样
# 继承之后,要自己划分要修改的区域
{% extends 'home.html' %}
# 1. 在模板中划定区域
{% block content %}
'''被划分的内容'''
{% endblock%}
# 2. 在子模板中
{% block content %}
'''被划分的内容'''
{% endblock%}
# 3. 任何一个模板的继承都应该有三块区域
1. css
2. js
3. html
# 导入
{% include 'welcome.html' %}
模型层
常见的十几种查询方法
filter all first update delete create
# res = models.UserInfo.objects.filter().all().first() # []
# try:
# print(res[0]) # jack1 object
# print(res[0].username)
# print(res[0].password)
# except :
# res = models.UserInfo.objects.filter().all().last() # []
#
# print(res.username)
# values value_list
# select id, username from table
# select *from t;
# 查询的结果是列表套字典
# res = models.UserInfo.objects.values('username','password','id') # 默认查询所有的字段,
# for i in res:
# print(i.username)
# 查询结果是列表套元组,其他的用法都跟values一样
# res = models.UserInfo.objects.values_list('username')
# print(res[0])
########################如何查看原生SQL语句
# res = models.UserInfo.objects.values('username', 'password', 'id')
# print(res) # QuerySet对象,他都有一个属性query
# SELECT "app01_userinfo"."username", "app01_userinfo"."password", "app01_userinfo"."id" FROM "app01_userinfo"
# print(res.query) # QuerySet对象,他都有一个属性query
# 增加、更新、删除
# object、行数、行数
# 第二种方式查看SQL语句
# res = models.UserInfo.objects.order_by('-id', 'age') # 默认是升序排列,如何降序
# res = models.UserInfo.objects.order_by('-id', '-age') # 默认是升序排列,如何降序
# print(res)
# 反转,先排序,数据要先有序才能翻转
# res=models.UserInfo.objects.all().order_by('-id').reverse()
# print(res)
# 查看数量的
# res = models.UserInfo.objects.count()
# print(res)
# exclude 排除
# res=models.UserInfo.objects.exclude(username='jack1')
# print(res)
#
# # exist
# res = models.UserInfo.objects.filter(pk=1).first()
# print(res)
测试环境的搭建
tests.py的用法
# 制作django环境
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'day58_dj.settings')
import django
django.setup()
查看原生sql语句
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'propagate': True,
'level': 'DEBUG',
},
}
}
基于双下滑线的查询
###############################基于双下划线的查询
# 1 年龄大于35岁的数据
# res = models.UserInfo.objects.filter(age__gte=35).all()
# print(res)
# # 年龄小于10岁的数据 equal
# res = models.UserInfo.objects.filter(age__lte=10).all()
# print(res)
# # 年龄是10 或者 20 或者 30
# res = models.UserInfo.objects.filter(age__in=[10, 20, 30]).all()
# print(res)
# # 年龄在18到40岁之间的 首尾都要
# from table where age between 18 and 40
# res = models.UserInfo.objects.filter(age__range=[18, 40])
# print(res)
# # 查询出名字里面含有s的数据 模糊查询
# from table where username like '%s%'
# res = models.UserInfo.objects.filter(username__contains='s').all()
# print(res)
# 用户名以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)
# 查询出注册时间是 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)
外键字段的查询
一对多的查询
# 图书和出版社是一对多的关系
增加一本图书?
# 外键字段的查询和增加
# 增加一本图书
# models.Book.objects.create(title='洋哥自转', price=1000, publish_date='2023-08-01', publish_id=1)
# publish_obj = models.Publish.objects.filter(pk=20).first()
try:
publish_obj = models.Publish.objects.get(pk=20)
except:
pass
# models.Book.objects.create(title='洋哥自转2', price=2000, publish_date='2023-08-02', publish=publish_obj)
# print(publish_obj) # None
# 删除一本图书
# models.Book.objects.filter(pk=1).delete()
# 修改
# models.Book.objects.filter(pk=2).update(publish=publish_obj)
# models.Book.objects.filter(pk=2).update(publish_id=3)