Django------filter查询的使用

一: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_404
from .models import Blog,Blogtype
from django.conf import settings
from django.core.paginator import Paginator
# Create your views here.
# EACH_OF_BLOGS_NUMBER = 7

def 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 contenx

def 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)

 

posted @ 2018-02-06 13:58  jeep-鹏  阅读(45963)  评论(0编辑  收藏  举报