题目示意图实现步骤
题目Django如何实现搜索词高亮显示
示意图 实现步骤核心思想:利用jieba分词,然后传回分词数组,注意转化成字符串返回,然后在js中使用split分成数组
test.html
{% extends "test_base.html" %} {% block content %}
搜索内容如下
{% for item in discussions %} {%if returnflag %}
{{ item.title }}
{%else%}
{{ item.title }}
{%endif%} {% endfor %}
{% endblock content %}
views.py
# test.html
def test_view(request):
searchQ = request.GET.get('searchQ',None)
if searchQ:
discussions = []
stop_words = word_cut(searchQ)
ds = Discussion.objects.all()
for d in ds:
for word in stop_words:
if word in d.title:
discussions.append(d)
discussions = list(set(discussions))
# 如果有搜索结果
if discussions:
returnflag = ','.join(stop_words)
else:
returnflag = None
else:
discussions = Discussion.objects.all()
returnflag = None
return render(request,"test.html",{'discussions':discussions,'returnflag':returnflag})
urls.py
path('test/',views.test_view),



