侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

django组合搜索实现过程详解(附代码)

框架(架构)  /  管理员 发布于 7年前   130

一.简介

  • # 组合搜索
  • # 技术方向:自动化,测试,运维,前端
  • # 分类:Python Linux JavaScript OpenStack Node.js GO
  • # 级别:初级 中级 高级 骨灰级

有4张表:

Direction(技术方向标),Classification(技术分类表),Level(难度级别表),Video(视频表)

它们的关系是:

Direction与Classification多对多关系

Video与Classification,Level是一对多关系

最终要实现的结果如下图:

二.models代码

class Direction(models.Model):  """  技术方向:自动化,测试,运维,前端  """  name = models.CharField(verbose_name='名称', max_length=32)  classification = models.ManyToManyField('Classification')  class Meta:    # 重命名表名,不要自动添加的app名    db_table = 'Direction'    verbose_name_plural = '方向(视频方向)'  def __str__(self):    return self.nameclass Classification(models.Model):  """  分类:Python Linux JavaScript OpenStack Node.js GO  """  name = models.CharField(verbose_name='名称', max_length=32)  class Meta:    db_table = 'Classification'    verbose_name_plural = '分类(视频分类)'  def __str__(self):    return self.nameclass Level(models.Model):  title = models.CharField(max_length=32)  class Meta:    db_table = 'Level'    verbose_name_plural = '难度级别'  def __str__(self):    return self.titleclass Video(models.Model):  status_choice = (    (1, '下线'),    (2, '上线'),  )  status = models.IntegerField(verbose_name='状态', choices=status_choice, default=1)  level = models.ForeignKey(Level)  classification = models.ForeignKey('Classification', null=True, blank=True)  weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)  title = models.CharField(verbose_name='标题', max_length=32)  summary = models.CharField(verbose_name='简介', max_length=32)  # img = models.ImageField(verbose_name='图片', upload_to='./static/images/Video/')  img = models.CharField(verbose_name='图片', max_length=32)  href = models.CharField(verbose_name='视频地址', max_length=256)  create_date = models.DateTimeField(auto_now_add=True)  class Meta:    db_table = 'Video'    verbose_name_plural = '视频'  def __str__(self):    return self.title

三.url路由代码

urlpatterns=[  url(r'^admin',admin.site.urls),  #利用的是有名分组的方法,分别获取不同的id  url(r'^video-(?P<direction_id>(\d+))-(?P<classification_id>(\d+))-(?P<level_id>(\d+)).html$', views.video,    name='video')]

四.视图代码

def video(request,*args,**kwargs):  condition = {}  for k, v in kwargs.items():    temp = int(v)    kwargs[k] = temp  print(kwargs) # (?P<direction_id>(\d+))-(?P<classification_id>(\d+))-(?P<level_id>(\d+))  # 构造查询字典  direction_id = kwargs.get('direction_id')  classification_id = kwargs.get('classification_id')  level_id = kwargs.get('level_id')  # 获取所有的技术方向  direction_list = models.Direction.objects.all()  # 当没有选择技术方向时,就获取所有分类  if direction_id == 0:    class_list = models.Classification.objects.all()    # 当没有选择分类时,不做什么    if classification_id == 0:      pass    else:      # 否则就将分类id放入字典      condition['classification_id'] = classification_id  else:    # 当选择了技术方向id时,查询出该技术方向下的所有分类    direction_obj = models.Direction.objects.filter(id=direction_id).first()    class_list = direction_obj.classification.all()    # 只获取该方向下的分类id    vlist = direction_obj.classification.all().values_list('id')    # 下面的代码为了生成condition是传入的一对多查询id,如:{'classification_id__in': (1, 2, 3), 'level_id': 1}    if not vlist:      classification_id_list = []    else:      # 将vlist转换成列表      classification_id_list = list(zip(*vlist))[0]    if classification_id == 0:      condition['classification_id__in'] = classification_id_list    else:      if classification_id in classification_id_list:        condition['classification_id'] = classification_id      else:        #指定技术方向:[1,2,3]  分类:5        kwargs['classification_id'] = 0        condition['classification_id__in'] = classification_id_list  if level_id == 0:    pass  else:    condition['level_id'] = level_id  level_list = models.Level.objects.all()  video_list = models.Video.objects.filter(**condition)  # 技术方向的queryset对象列表  print(direction_list)  # 分类的queryset对象列表  print(class_list)  # 等级的queryset对象列表  print(level_list)  # video的queryset对象列表  print(video_list)  # 技术方向的id,分类的id,等级的id组成的字典  print(kwargs)  return render(    request,    'video.html',    {      'direction_list':direction_list,      'class_list':class_list,      'level_list':level_list,      'video_list':video_list,      'kwargs':kwargs,    }  )

五.模板代码

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <style>    .condition a {      display: inline-block;      padding: 5px 8px;      border: 1px solid #dddddd;    }    .condition a.active {      background-color: coral;      color: white;    }  </style></head><body><div class="condition">  <h1>筛选</h1>  <div>    {% if kwargs.direction_id == 0 %}      {#反向解析#}      <a href="https:/article/{% url " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" video" direction_id=0 classification_id=kwargs.classification_id level_id=kwargs.level_id %}"        class="active">全部</a>    {% else %}      <a href="https:/article/{% url " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" video" direction_id=0 classification_id=kwargs.classification_id level_id=kwargs.level_id %}">全部</a>    {% endif %}    {% for item in direction_list %}      {% if item.id == kwargs.direction_id %}        <a href="https:/article/{% url " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" video" direction_id=item.id classification_id=kwargs.classification_id level_id=kwargs.level_id %}"          class="active">{{ item.name }}</a>      {% else %}        <a href="https:/article/{% url " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" video" direction_id=item.id classification_id=kwargs.classification_id level_id=kwargs.level_id %}">{{ item.name }}</a>      {% endif %}    {% endfor %}  </div>  <div>    {% if kwargs.classification_id == 0 %}      <a href="https:video-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html" rel="external nofollow" rel="external nofollow" class="active">全部</a>    {% else %}      <a href="https:video-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html" rel="external nofollow" rel="external nofollow" >全部</a>    {% endif %}    {% for item in class_list %}      {% if item.id == kwargs.classification_id %}        <a href="https:video-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html" rel="external nofollow" rel="external nofollow"           class="active">{{ item.name }}</a>      {% else %}        <a href="https:video-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html" rel="external nofollow" rel="external nofollow" >{{ item.name }}</a>      {% endif %}    {% endfor %}  </div>  <div>    {% if kwargs.level_id == 0 %}      <a href="https:video-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html" rel="external nofollow" rel="external nofollow" class="active">全部</a>    {% else %}      <a href="https:video-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html" rel="external nofollow" rel="external nofollow" >全部</a>    {% endif %}    {% for item in level_list %}      {% if item.id == kwargs.level_id %}        <a href="https:video-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}.html" rel="external nofollow" rel="external nofollow"           class="active">{{ item.title }}</a>      {% else %}        <a href="https:video-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}.html" rel="external nofollow" rel="external nofollow" >{{ item.title }}</a>      {% endif %}    {% endfor %}  </div></div><div>  <h1>结果</h1>  {% for row in video_list %}    <div>{{ row.title }}</div>  {% endfor %}</div></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    django基于cors解决跨域请求问题详解
    下一条:
    Django RBAC权限管理设计过程详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客