美文网首页
django 实现搜索功能

django 实现搜索功能

作者: 叶叶阿姨 | 来源:发表于2020-01-09 15:56 被阅读0次

    老规矩 自己看

    def api_back_search_news(request):
        # 搜索
        if request.method == 'POST':
            cursor = connection.cursor()
            search = unquote(request.POST.get('value'))
            page = int(request.POST.get('page', 1))
            limit = int(request.POST.get('limit', 10))
            error_msg = ''
            if not search:
                error_msg = '请输入关键词'
            cursor.execute(f'select * from quick_poll_alerts where locate("{search}",alerts_title) order by alerts_releasetime desc limit '
                           f'{(page - 1) * limit},{limit}')
            data = [format_alerts_list(i, search) for i in cursor.fetchall()]
            cursor.execute(f'select count(*) from quick_poll_alerts where locate("{search}",alerts_title)')
            count = cursor.fetchall()
            print(data)
            print(count)
            cursor.close()
            return JsonResponse({'msg': '添加成功', 'code': 200, 'data': data, 'count': count})
    

    这里的format_alerts_list

    def format_alerts_list(data, key=None, type=None, label=None):
        # 格式化数据
        ret = {}
        # id
        ret['id'] = data[0]
        # img
        ret['img'] = data[3]
        # title
        ret['title'] = data[12]
        # abstract
        ret['abstract'] = data[2]
        # niche
        ret['niche'] = Niche.objects.filter(id=data[6]).first().niche_name
        # article
        ret['article'] = Article.objects.filter(id=data[4]).first().alerts_section_name
        # time
        ret['time'] = data[11].strftime("%Y-%m-%d")
        # source
        ret['source'] = Source.objects.filter(id=data[10]).first().source_name
        # regional
        ret['regional'] = Regional.objects.filter(id=data[7]).first().regional_name
        # content
        if type == 'detail':
            ret['content'] = data[1]
        if label:
            ret['label'] = data[13]
        return ret
    

    相关文章

      网友评论

          本文标题:django 实现搜索功能

          本文链接:https://www.haomeiwen.com/subject/hnwyactx.html