django-实现查询

Posted tangpg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django-实现查询相关的知识,希望对你有一定的参考价值。

django Q模块

from django.db.models import Q


def search(request):
    q = request.GET.get(q)
    if q: # 查询字段不能为None,否者报错
        # title或者content中包含了搜索的关键字
        newses = News.objects.filter(Q(title__icontains=q)|Q(content__icontains=q))  
        context = {
            newses: newses
        }
    else:
        context = {}
    return render(request,news/search.html,context=context)

更多的查询方法: http://www.cnblogs.com/tangpg/p/9010610.html

以上是关于django-实现查询的主要内容,如果未能解决你的问题,请参考以下文章