1 普通正则

2 分组正则  

url(r'/blog/(\d+)/(\d+)',views.blog)     blog(request,arq1,arq2) 按照位置传参

3 分组命名

url(r'/blog/(?P<year>\d+)/(?P<month>\d+)',views.blog)     blog(request,year,month)

4  用name 指定别名

url(r'/blog/(?P<year>\d+)/(?P<month>\d+)',views.blog,name ='blog')     blog(request,year,month

涉及到了反向解析URL

4-1 在html里面:{%  url 'blog' 2012 12%}

4-2from django.urls import reverse reverse ('blog',arg=(2013,11))

5 用namespace 指定命名空间
url(r'/app01/',inclue(app01.urls,namespace='app01)) 
 涉及到了反向解析URL
5-1 HTML 里面{% url 'app01:blog' 2012 11%}
5-22 在views reverse('app01:blog' ,arg=(2010,33))

 --------------

通过get取值 和通关过分组取值的用法和对比

 

get 和分组取值

 

1 HTML 里面{% url 'app01:blog' 2012 11%}
2 在views reverse('app01:blog' ,arg=(2010,33))

要在Django Console 打印出有sq语句 在django 项目的配置信息中加上这个日志 而且引用

 

logging

 

 

models 中插入这句在定义的类的下面的缩进行里 这样打印出来的结果对象就会是实际的内容
  def __str__(self):
    return '{}{}'.format(self.cname,self.first_day)
单表查询的增删改查 
单表查询的API 介绍 共13条
1 返回QuerySet对象的有8条
1 all():全部 2 filter():过滤 3 exclude():不包括 
4 value()值 5value_list() 值的列表 6 order_by() 排序正序
7 reverse() 反转 8 distinct() 去重

 

 

 


models.Ban_list.objects.filter(id=7)
<QuerySet [<Ban_list: Linux 1212期2015-01-29>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` = 7 LIMIT 21; args=(7,)
(0.000) SELECT VERSION(); args=None
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` = 7 LIMIT 21; args=(7,)

 

.all()
filter() 支持链式操作
exclude() 不包含
.value() 单列 多列 是字典
.value_list() 元祖
.order_by() 排序
all().order_by('id)默认id排序 -id反序
.reverse() 和order_by('-id)
.distinct() 去重 保留前者

2返回数字的: 1 count():计数行数

.count() 计算多少行

3返回布尔值的:1exists():是否存在


4 返回的是对象的:1 get() 得到的是一个对象 2 first():第一个对象 3 last() :最后一个对象

.get() 取单个对象
.first() .last() 对象

 1 Queryset 对象8个

 

models.Ban_list.objects.get(id=7)
(0.001) SELECT @@SQL_AUTO_IS_NULL; args=None
(0.001) SELECT VERSION(); args=None
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` = 7; args=(7,)
<Ban_list: Linux 1212期2015-01-29>


models.Ban_list.objects.all()
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` LIMIT 21; args=()
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>]>
(0.001) SELECT @@SQL_AUTO_IS_NULL; args=None
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` LIMIT 21; args=()


models.Ban_list.objects.filter(id=7)
<QuerySet [<Ban_list: Linux 1212期2015-01-29>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` = 7 LIMIT 21; args=(7,)
(0.000) SELECT VERSION(); args=None
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` = 7 LIMIT 21; args=(7,)


models.Ban_list.objects.filter(cname='大数据1班').first()
<Ban_list: 大数据1班2020-12-01>
(0.061) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`cname` = '大数据1班' ORDER BY `app0001_ban_list`.`id` ASC LIMIT 1; args=('大数据1班',)
models.Ban_list.objects.exclue(cname='大数据1班')
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'Manager' object has no attribute 'exclue'


models.Ban_list.objects.exclude(cname='大数据1班')
(0.005) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)


models.Ban_list.objects.exclude(cname='大数据1班').values('cname')
<QuerySet [{'cname': 'Python 18期'}, {'cname': 'Linux 1212期'}, {'cname': 'Python 11期'}, {'cname': 'Linux 4期'}, {'cname': 'Python 19期'}, {'cname': '大数据2班'}]>
(0.002) SELECT `app0001_ban_list`.`cname` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)
(0.001) SELECT `app0001_ban_list`.`cname` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)


models.Ban_list.objects.exclude(cname='大数据1班').values('cname','first_day')
(0.001) SELECT `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)
<QuerySet [{'cname': 'Python 18期', 'first_day': datetime.date(2023, 10, 19)}, {'cname': 'Linux 1212期', 'first_day': datetime.date(2015, 1, 29)}, {'cname': 'Python 11期', 'first_day': datetime.date(2012, 9, 1)}, {'cname': 'Linux 4期', 'first_day': datetime.date(2019, 12, 23)}, {'cname': 'Python 19期', 'first_day': datetime.date(2016, 10, 19)}, {'cname': '大数据2班', 'first_day': datetime.date(2017, 10, 28)}]>
(0.001) SELECT `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)


models.Ban_list.objects.exclude(cname='大数据1班').values_list('cname','first_day')
(0.003) SELECT `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)
<QuerySet [('Python 18期', datetime.date(2023, 10, 19)), ('Linux 1212期', datetime.date(2015, 1, 29)), ('Python 11期', datetime.date(2012, 9, 1)), ('Linux 4期', datetime.date(2019, 12, 23)), ('Python 19期', datetime.date(2016, 10, 19)), ('大数据2班', datetime.date(2017, 10, 28))]>
(0.001) SELECT `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') LIMIT 21; args=('大数据1班',)


models.Ban_list.objects.exclude(cname='大数据1班').order_by('first_day')
<QuerySet [<Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 18期2023-10-19>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') ORDER BY `app0001_ban_list`.`first_day` ASC LIMIT 21; args=('大数据1班',)
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE NOT (`app0001_ban_list`.`cname` = '大数据1班') ORDER BY `app0001_ban_list`.`first_day` ASC LIMIT 21; args=('大数据1班',)


models.Ban_list.objects.all().order_by('first_day')
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`first_day` ASC LIMIT 21; args=()
<QuerySet [<Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: 大数据1班2018-01-10>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Python 18期2023-10-19>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`first_day` ASC LIMIT 21; args=()


models.Ban_list.objects.all()
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` LIMIT 21; args=()
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: 大数据1班2018-01-10>]>
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` LIMIT 21; args=()


models.Ban_list.objects.all().order_by('id')
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` ASC LIMIT 21; args=()
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: 大数据1班2018-01-10>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` ASC LIMIT 21; args=()


models.Ban_list.objects.all().order_by('-id')
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` DESC LIMIT 21; args=()
<QuerySet [<Ban_list: 大数据1班2018-01-10>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: Python 19期2016-10-19>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 11期2012-09-01>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: Python 18期2023-10-19>]>
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` DESC LIMIT 21; args=()


models.Ban_list.objects.all().reverse()
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: 大数据1班2018-01-10>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` LIMIT 21; args=()
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` LIMIT 21; args=()


models.Ban_list.objects.all().order_by('id').reverse()
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` DESC LIMIT 21; args=()
<QuerySet [<Ban_list: 大数据1班2018-01-10>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: Python 19期2016-10-19>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 11期2012-09-01>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: Python 18期2023-10-19>]>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` DESC LIMIT 21; args=()


models.Ban_list.objects.values('cname').distinct()
(0.003) SELECT DISTINCT `app0001_ban_list`.`cname` FROM `app0001_ban_list` LIMIT 21; args=()
<QuerySet [{'cname': 'Python 18期'}, {'cname': 'Linux 1212期'}, {'cname': '大数据1班'}, {'cname': 'Python 11期'}, {'cname': 'Linux 4期'}, {'cname': 'Python 19期'}, {'cname': '大数据2班'}]>
(0.002) SELECT DISTINCT `app0001_ban_list`.`cname` FROM `app0001_ban_list` LIMIT 21; args=()

 

2 数字的一个
models.Ban_list.objects.all().count()
8
(0.001) SELECT COUNT(*) AS `__count` FROM `app0001_ban_list`; args=()

3对象的3个
models.Ban_list.objects.all().first()
<Ban_list: Python 18期2023-10-19>
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` ASC LIMIT 1; args=()


models.Ban_list.objects.all().last()
<Ban_list: 大数据1班2018-01-10>
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` ORDER BY `app0001_ban_list`.`id` DESC LIMIT 1; args=()
models.Ban_list.objects.get(id=7)
<Ban_list: Linux 1212期2015-01-29>
(0.002) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` = 7; args=(7,)

 

 

逻辑运算

models.Ban_list.objects.filter(id__gt=1)   大于
(0.076) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` > 1 LIMIT 21; args=(1,)
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>, <Ban_list: 大数据1班2020-12-01>, <Ban_list: Python 11期2012-09-01>, <Ban_list: Linux 4期2019-12-23>, <Ban_list: Python 19期2016-10-19>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: 大数据1班2018-01-10>]>
(0.014) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` > 1 LIMIT 21; args=(1,)


models.Ban_list.objects.filter(id__lt=10)  小于
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>]>
(0.017) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` < 10 LIMIT 21; args=(10,)
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` < 10 LIMIT 21; args=(10,)


models.Ban_list.objects.filter(id__in=[2,7,2]) 包含里面
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` IN (2, 7) LIMIT 21; args=(2, 7)
<QuerySet [<Ban_list: Python 18期2023-10-19>, <Ban_list: Linux 1212期2015-01-29>]>
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`id` IN (2, 7) LIMIT 21; args=(2, 7)

查找

models.Ban_list.objects.filter(cname__contains='大数据')    模糊查找
(0.030) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`cname` LIKE BINARY '%大数据%' LIMIT 21; args=('%大数据%',)
<QuerySet [<Ban_list: 大数据1班2020-12-01>, <Ban_list: 大数据2班2017-10-28>, <Ban_list: 大数据1班2018-01-10>]>
(0.001) SELECT `app0001_ban_list`.`id`, `app0001_ban_list`.`cname`, `app0001_ban_list`.`first_day` FROM `app0001_ban_list` WHERE `app0001_ban_list`.`cname` LIKE BINARY '%大数据%' LIMIT 21; args=('%大数据%',)

不区分大小写的cname__icontains

models.Ban_list.objeces.filter(cname_icontains=''