You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 导入日志模块,用于记录日志信息
import logging
# 导入Django表单模块
from django import forms
# 从haystack.forms导入SearchForm, 用于实现搜索功能
from haystack . forms import SearchForm
# 创建日志记录器
logger = logging . getLogger ( __name__ )
# 定义博客搜索表单类, 继承自Haystack的SearchForm
class BlogSearchForm ( SearchForm ) :
# 定义搜索查询字段,设置为必填项
querydata = forms . CharField ( required = True )
# 重写search方法, 实现自定义搜索逻辑
def search ( self ) :
# 调用父类的search方法获取搜索结果
datas = super ( BlogSearchForm , self ) . search ( )
# 检查表单数据是否有效
if not self . is_valid ( ) :
# 如果无效,返回无查询结果
return self . no_query_found ( )
# 如果查询数据存在
if self . cleaned_data [ ' querydata ' ] :
# 记录查询关键字到日志中
logger . info ( self . cleaned_data [ ' querydata ' ] )
# 返回搜索结果
return datas