zoukankan      html  css  js  c++  java
  • Django模糊查询

    https://blog.csdn.net/liuweiyuxiang/article/details/71104613

    def search(request):

        searchtype = request.POST.get("searchtype")

        keyword = request.POST.get("keyword")

        if searchtype == "all":

            #多个字段模糊查询, 括号中的下划线是双下划线,双下划线前是字段名,双下划线后可以是icontains或contains,区别是是否大小写敏感,竖线是或的意思

            sciencenews = models.Sciencenews.objects.filter(Q(title__icontains=keyword)

    |Q(content__icontains=keyword)|Q(author__icontains=keyword))

        elif searchtype == "author":

            #单个字段模糊查询

            sciencenews = models.Sciencenews.objects.filter(author__icontains=keyword)

        elif searchtype == "title":

            sciencenews = models.Sciencenews.objects.filter(title__icontains=keyword)

        elif searchtype == "content":

            sciencenews = models.Sciencenews.objects.filter(content__icontains=keyword)

    else:

    #使用点连接的filter链表示and

    sciencenews = models.Sciencenews.objects.filter(author__icontains=keyword).

    filter(title__icontains=keyword).filter(content__icontains=keyword)

        return render(request,"show/index.html",{"param":sciencenews,"searchtype":searchtype,"keyword":keyword})

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

  • 相关阅读:
    第十六节,基本数据类型,字典dict
    第十五节,基本数据类型,元组tuple
    第十四节,基本数据类型,列表list
    liunx rm 命令修改
    linux 创建用户和密码
    linux 权限
    system
    一个tomcat上部署多个项目,并通过不同端口号访问不同的项目
    Java 清理和垃圾回收
    static 方法
  • 原文地址:https://www.cnblogs.com/pythonClub/p/9886579.html
Copyright © 2011-2022 走看看