Python Web开发:基于模糊查询的新闻标题搜索

来自CloudWiki
跳转至: 导航搜索

背景

本节将通过Django实现新闻搜索功能。

检索依据难易程度 分为

  • 基于模糊查询的新闻标题搜索
  • 基于haystack的全文高级搜索

模糊搜索 类似于mysql中的 name like '%wang%',但是查询效率低

还有一种全文搜索。

实训步骤

添加搜索表单

newList.html:

            <div class="model-details-title">
                {{newName}}
                <div class="col-md-7 hidden-xs model-details-title-search">
                    <form method="get" action="{% url 'newsApp:search' %}">
                        {% csrf_token %}
                        <div class="input-group">
                            <input type="text" name="keyword" class="form-control" placeholder="请输入关键词" required />
                            <span class="input-group-btn">
                                <input type="submit" class="btn btn-default" value="查询" />
                            </span>
                        </div>
                    </form>
                </div>
            </div>

提交方式为get ,如果提交方式为post,则需要在视图函数前添加require_POST装饰器。

news.css添加:

* 新闻搜索框 */
.model-details-title-search{
	font-size:18px;
	width: 300px;
	float: right;
	margin-bottom: 20px;
}

添加响应处理函数

在视图文件newsApp/views.py 添加新闻搜索对应的响应处理函数:

def search(request):
    keyword = request.GET.get('keyword')
    newList = MyNew.objects.filter(title__icontains=keyword)
    newName = "关于 " + "\"" + keyword + "\"" + " 的搜索结果"
    return render(request, 'searchList.html', {
        'active_menu': 'news',
        'newName': newName,
        'newList': newList,
    })

制作新闻搜索页面

searchList.html:

{% extends "base.html" %}
{% load staticfiles %}
{% block title %}
{{newName}}
{% endblock %}
{% block content %}
<link href="{% static 'css/news.css' %}" rel="stylesheet">
<!-- 广告横幅 -->
<div class="container-fluid">
    <div class="row">
        <img class="img-responsive model-img" src="{% static 'img/new.jpg' %}">
    </div>
</div>
<!-- 主体内容 -->
<div class="container">
    <div class="row row-3">
        <div class="model-details-title">
            {{newName}}
            <div class="col-md-7 hidden-xs model-details-title-search">
                <form method="get" action="/search/">
                    {% csrf_token %}
                    <div class="input-group">
                        <input type="text" name="keyword" class="form-control" placeholder="请输入关键词" required />
                        <span class="input-group-btn">
                            <input type="submit" class="btn btn-default" value="查询" />
                        </span>
                    </div>
                </form>
            </div>
        </div>
        <div class="model-details">
            {% for mynew in newList %}
            <div class="news-model">
                <img src="{% static 'img/newsicon.gif' %}">
                <a href="{% url 'newsApp:newDetail' mynew.id %}"><b>{{mynew.title}}</b></a>
                <span>【{{mynew.publishDate|date:"Y-m-d"}}】</span>
            </div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %}

配置路由

newsApp/urls.py:

from django.urls import path
from . import views

app_name = 'newsApp'

urlpatterns = [
    path('news/<str:newName>/', views.news, name='news'),#新闻列表
    path('newDetail/<int:id>/', views.newDetail, name='newDetail'),#新闻详情
    path('search/',views.search,name='search'),
    
]

效果图

Python21052001.png

Python21052002.png