当前位置:网站首页>搜索引擎实现关键词高亮显示
搜索引擎实现关键词高亮显示
2022-07-18 05:04:00 【Mr_Joer】
实现关键词高亮我们的思路是用html语句。即将关键词用html语句包裹起来,其中常用的语句有<span style="font-weght:bold"></span>、<span style="color:xxx"></span>,其中xxx可以放入常见颜色的英文名字(white),或者十六进制颜色代码(#ffffff)。
查询以及获取数据集data的代码如下:
此时data中的数据结构如下:
可见所查询数据(最右边)按照行被封装成了一个个Message对象,拼接成一个列表list,最后放入QuerySet数据集data中。
现在要将每一条数据的title、info字段中关键字用html包裹,想到了replace函数(替换指定的字符串);又因为Django处于安全考虑,默认输出的html语句不生效,就要用到django.utils.safestring下的make_safe方法,代码如下:
for j in range(0,keyCount):
dict['list'+str(i)].title = mark_safe(dict['list'+str(i)].title.replace(key[j],'<span style="color:red;">%s</span>'%key[j]))
dict['list'+str(i)].info = mark_safe(dict['list'+str(i)].info.replace(key[j],'<span style="color:red;">%s</span>'%key[j]))
其中dict['list'+str(i)]是我将Paginator分页处理后的数据封装进了一个字典中,键值对中的值就是一个个Message类;key[j]是当前关键词,通过keyCount次循环以将所有关键词前后均加入html代码,最后的视觉效果如下:
边栏推荐
猜你喜欢
Golang signal. Exit of notify signal
Knapsack problem (introductory basic version)
"Schedulemaster" can be used quickly Net core based open source distributed task scheduling platform
Pyqt5 small case
JSR303检验
Template method model of behavioral model
QoS服务质量一QoS概述
input数字自动分割Js插件
If it explodes, the redise pulled down by the warehouse can be backstage, but its own export can't be backstage!
Method “watch“ has type “object“ in the component definition. Did you reference the function correct
随机推荐
Leetcode 1286. 字母组合迭代器(网友思路)
时间戳转换格林威治时间
机器学习练习 7 - K-means 和PCA(主成分分析)
cdc 对于oracle集群模式应该怎么配置呀 我在单机模式下可以正常运行,集群模式下就读取不到增
力扣(LeetCode)198. 打家劫舍(2022.07.17)
从0开始的 TypeScriptの十四:内置工具类型
el-table选中后跳转页面保持选中
聪明人离职,怎么能不得罪人?
区间dp(入门基础版)
归并排序(递归和非递归写法)
Ten design principles teach you how to design web page layout
今日直播|Apache Pulsar Meetup:vivo、腾讯云、BIGO、云兴科技实践分享
代码块的理解学习
绪论
Common AI frameworks and tools 11 Deploy MNIST handwritten numeral recognition to local web based on tensorflow (keras) +flask
Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
[Luogu] p3901 sequence to find a difference
Roson的Qt之旅#97 Qt编译报错-Could not determine which “make“ command to run
CompletableFuture异步编排
2022.7.10DAY602