博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django------filter查询的使用
阅读量:4946 次
发布时间:2019-06-11

本文共 9149 字,大约阅读时间需要 30 分钟。

一:filter查询可以方便的查询出我们需要使用的信息:

def blogdetail(request,aricle_id):    contenx = {}    detail = contenx['detail'] = get_object_or_404(Blog,pk = aricle_id)    contenx['blog_previous'] = Blog.objects.filter(create_time__gt = detail.create_time).last()#得到上一页信息    contenx['blog_next'] = Blog.objects.filter(create_time__lt = detail.create_time).first()#下一页信息    contenx['detail'] =detail    return render_to_response('blog/detail.html',contenx)

 一、下面来看看:filter查询:

1.__contains(包含)

shell命令下查询:Blog.objects.filter(title__contains ="django")------------------>返回一个queryset[]查询(查询集)只能输入一个值。加上一个"i"后不区别大小写【sql等数据库中】

2.__in (其中之一,可以传入一个列表,传多个值。)

Blog.objects.filter(id__in = [3,6,9])

3.__range(一个范围,使用元祖):

> Blog.objects.filter(id__range =(30,45))

二、前面讲的是包含的查询,下面来看看不包含的查询:

exclude方法为不包含的:

 Blog.objects.exclude(id=3)

三、日期类型查询

Blog.objects.filter(create_time__year = 2018)------------->查询年为2018年的内容

Blog.objects.filter(create_time__month=3)----------------->查询月

 

四、日期查询类型只是筛选了条件,怎么让条件在前端显示呢?django提供了一个dates方法:

Blog.objects.dates('create_time', 'year','DESC')--------------------->分别为:时间字段,查询返回年year,年月month,年月日day,最后一个参数是排序方式ASC正序,DESC倒序。

def blog_with_date(request,year,month):    contenx={}    blogs= Blog.objects.filter(create_time__year = year ,create_time__month = month)    paginator = Paginator(blogs, settings.EACH_OF_BLOGS_NUMBER)#每10篇文章分一页    page_num = request.GET.get('page',1)#GET方法获得url参数?page1 page是参数需要带引号    page_of_blogs = paginator.get_page(page_num)#get_page方法处理用户输入的错误值    current_page_num = page_of_blogs.number#获得当前页    # 比较截取到想要的那个数到最小值1时用小的值和1作比较生成一个从小到当前的值    range_page = list(range(max(current_page_num-2,1),current_page_num))+\                list(range(current_page_num, min(current_page_num+2,paginator.num_pages)+1))    # range(当前到---->想要的那个值取到最后最小的那个值)    #添加省略号    if range_page[0]-1>=2:        range_page.insert(0,'...')    if paginator.num_pages-range_page[-1]>=2:        range_page.append('...')    #添加首页和尾页    if range_page[0]!=1:        range_page.insert(0,1)    if range_page[-1]!=paginator.num_pages:        range_page.append(paginator.num_pages)    contenx['blogs_with_date'] = '%s年%s月'%(year,month)    contenx['blogs'] = page_of_blogs.object_list    contenx['page_of_blogs'] =  page_of_blogs    contenx['range_page'] = range_page    contenx['blogtypes']= Blogtype.objects.all()#获取所有的分类    contenx['blog_dates'] = Blog.objects.dates('create_time','month','DESC')    return render_to_response('blog/blog_with_date.html',contenx)

 五、代码整理公共部分提取

from django.shortcuts import render_to_response,get_object_or_404from .models import Blog,Blogtypefrom django.conf import settingsfrom django.core.paginator import Paginator# Create your views here.# EACH_OF_BLOGS_NUMBER = 7def get_blog_artacile_common_date(request,blogs):    paginator = Paginator(blogs, settings.EACH_OF_BLOGS_NUMBER)#每10篇文章分一页    page_num = request.GET.get('page',1)#GET方法获得url参数?page1 page是参数需要带引号    page_of_blogs = paginator.get_page(page_num)#get_page方法处理用户输入的错误值    current_page_num = page_of_blogs.number#获得当前页    # 比较截取到想要的那个数到最小值1时用小的值和1作比较生成一个从小到当前的值    range_page = list(range(max(current_page_num-2,1),current_page_num))+\                list(range(current_page_num, min(current_page_num+2,paginator.num_pages)+1))    # range(当前到---->想要的那个值取到最后最小的那个值)    #添加省略号    if range_page[0]-1>=2:        range_page.insert(0,'...')    if paginator.num_pages-range_page[-1]>=2:        range_page.append('...')    #添加首页和尾页    if range_page[0]!=1:        range_page.insert(0,1)    if range_page[-1]!=paginator.num_pages:        range_page.append(paginator.num_pages)    contenx = {}    contenx['page_of_blogs'] =  page_of_blogs    contenx['blogs'] = page_of_blogs.object_list#获取符合参数所有的文章    # contenx['count'] = Blog.objects.all().count()    contenx['blog_dates'] = Blog.objects.dates('create_time','month','DESC')    contenx['range_page'] = range_page    contenx['blogtypes'] = Blogtype.objects.all()#获取所有的分类    return contenxdef bloglist(request):    blogs = Blog.objects.all()    contenx = get_blog_artacile_common_date(request,blogs)    return render_to_response('blog/list.html',contenx)def blog_with_type(request,typename_pk):    typename = get_object_or_404(Blogtype,pk = typename_pk)#拿到传入的类型参数    blogs = Blog.objects.filter(typename=typename)    contenx =get_blog_artacile_common_date(request,blogs)    contenx['typename'] = typename#显示分类列    return render_to_response('blog/blog_with_type.html',contenx)def blog_with_date(request,year,month):    blogs= Blog.objects.filter(create_time__year = year ,create_time__month = month)    contenx=get_blog_artacile_common_date(request,blogs)    contenx['blogs_with_date'] = '%s年%s月'%(year,month)    return render_to_response('blog/blog_with_date.html',contenx)def blogdetail(request,aricle_id):    contenx = {}    detail = get_object_or_404(Blog,pk = aricle_id)    contenx['blog_previous'] = Blog.objects.filter(create_time__gt = detail.create_time).last()#得到上一页信息    contenx['blog_next'] = Blog.objects.filter(create_time__lt = detail.create_time).first()#下一页信息    contenx['detail'] =detail    return render_to_response('blog/detail.html',contenx)

原来代码:

def bloglist(request):    blogs = Blog.objects.all()    paginator = Paginator(blogs, settings.EACH_OF_BLOGS_NUMBER)#每10篇文章分一页    page_num = request.GET.get('page',1)#GET方法获得url参数?page1 page是参数需要带引号    page_of_blogs = paginator.get_page(page_num)#get_page方法处理用户输入的错误值    current_page_num = page_of_blogs.number#获得当前页    # 比较截取到想要的那个数到最小值1时用小的值和1作比较生成一个从小到当前的值    range_page = list(range(max(current_page_num-2,1),current_page_num))+\                list(range(current_page_num, min(current_page_num+2,paginator.num_pages)+1))    # range(当前到---->想要的那个值取到最后最小的那个值)    #添加省略号    if range_page[0]-1>=2:        range_page.insert(0,'...')    if paginator.num_pages-range_page[-1]>=2:        range_page.append('...')    #添加首页和尾页    if range_page[0]!=1:        range_page.insert(0,1)    if range_page[-1]!=paginator.num_pages:        range_page.append(paginator.num_pages)    contenx = {}    contenx['page_of_blogs'] =  page_of_blogs    contenx['blogs'] = page_of_blogs.object_list#获取符合参数所有的文章    # contenx['count'] = Blog.objects.all().count()    contenx['blog_dates'] = Blog.objects.dates('create_time','month','DESC')    contenx['range_page'] = range_page    contenx['blogtypes'] = Blogtype.objects.all()#获取所有的分类    return render_to_response('blog/list.html',contenx)def blog_with_type(request,typename_pk):    contenx ={}    typename = get_object_or_404(Blogtype,pk = typename_pk)#拿到传入的类型参数    blogs = Blog.objects.filter(typename=typename)    paginator = Paginator(blogs, settings.EACH_OF_BLOGS_NUMBER)#每10篇文章分一页    page_num = request.GET.get('page',1)#GET方法获得url参数?page1 page是参数需要带引号    page_of_blogs = paginator.get_page(page_num)#get_page方法处理用户输入的错误值    current_page_num = page_of_blogs.number#获得当前页    # 比较截取到想要的那个数到最小值1时用小的值和1作比较生成一个从小到当前的值    range_page = list(range(max(current_page_num-2,1),current_page_num))+\                list(range(current_page_num, min(current_page_num+2,paginator.num_pages)+1))    # range(当前到---->想要的那个值取到最后最小的那个值)    #添加省略号    if range_page[0]-1>=2:        range_page.insert(0,'...')    if paginator.num_pages-range_page[-1]>=2:        range_page.append('...')    #添加首页和尾页    if range_page[0]!=1:        range_page.insert(0,1)    if range_page[-1]!=paginator.num_pages:        range_page.append(paginator.num_pages)    contenx['page_of_blogs'] =  page_of_blogs    contenx['blogs'] = page_of_blogs.object_list#获取符合参数所有的文章    # contenx['count'] = Blog.objects.all().count()    contenx['range_page'] = range_page    contenx['typename'] = typename#显示分类列    contenx['blogtypes']= Blogtype.objects.all()#获取所有的分类    contenx['blog_dates'] = Blog.objects.dates('create_time','month','DESC')    return render_to_response('blog/blog_with_type.html',contenx)def blog_with_date(request,year,month):    contenx={}    blogs= Blog.objects.filter(create_time__year = year ,create_time__month = month)    paginator = Paginator(blogs, settings.EACH_OF_BLOGS_NUMBER)#每10篇文章分一页    page_num = request.GET.get('page',1)#GET方法获得url参数?page1 page是参数需要带引号    page_of_blogs = paginator.get_page(page_num)#get_page方法处理用户输入的错误值    current_page_num = page_of_blogs.number#获得当前页    # 比较截取到想要的那个数到最小值1时用小的值和1作比较生成一个从小到当前的值    range_page = list(range(max(current_page_num-2,1),current_page_num))+\                list(range(current_page_num, min(current_page_num+2,paginator.num_pages)+1))    # range(当前到---->想要的那个值取到最后最小的那个值)    #添加省略号    if range_page[0]-1>=2:        range_page.insert(0,'...')    if paginator.num_pages-range_page[-1]>=2:        range_page.append('...')    #添加首页和尾页    if range_page[0]!=1:        range_page.insert(0,1)    if range_page[-1]!=paginator.num_pages:        range_page.append(paginator.num_pages)    contenx['blogs_with_date'] = '%s年%s月'%(year,month)    contenx['blogs'] = page_of_blogs.object_list    contenx['page_of_blogs'] =  page_of_blogs    contenx['range_page'] = range_page    contenx['blogtypes']= Blogtype.objects.all()#获取所有的分类    contenx['blog_dates'] = Blog.objects.dates('create_time','month','DESC')    return render_to_response('blog/blog_with_date.html',contenx)

 

转载于:https://www.cnblogs.com/jeepzp/p/8418627.html

你可能感兴趣的文章
打印图形
查看>>
ngx_http_core_module 模块
查看>>
两个常见的oracle索引
查看>>
【蓝桥杯】入门训练 Fibonacci数列
查看>>
实验十 指针2
查看>>
[python]pickle和cPickle
查看>>
剑指Offer--二叉树的镜像
查看>>
PAT-BASIC-1031-查验身份证
查看>>
连连看小游戏
查看>>
(180905)如何通过梯度下降法降低损失----Google机器学习速成课程笔记
查看>>
面试介绍项目经验(转)
查看>>
<metro>Google的验证
查看>>
Oracle 表的分组操作
查看>>
在OS X上的Intllij Idea中配置GlassFish
查看>>
用查表法快速转换yv12到RGB【转】
查看>>
使用公钥登录SSL
查看>>
hdu 1290_献给杭电五十周年校庆的礼物
查看>>
豆瓣电影api
查看>>
BufferedInputStream和FileInputStream的区别
查看>>
likely() 和 unlikely()
查看>>