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.
# bjy: 导入日志模块
import logging
# bjy: 从Django中导入表单模块
from django import forms
# bjy: 从haystack( 一个Django搜索框架) 中导入基础搜索表单
from haystack . forms import SearchForm
# bjy: 获取一个名为__name__的logger实例, 用于记录日志
logger = logging . getLogger ( __name__ )
# bjy: 定义一个自定义的博客搜索表单, 继承自Haystack的SearchForm
class BlogSearchForm ( SearchForm ) :
# bjy: 定义一个名为querydata的字符字段, 用于接收用户输入的搜索关键词, 并设置为必填
querydata = forms . CharField ( required = True )
# bjy: 重写search方法, 用于执行搜索逻辑
def search ( self ) :
# bjy: 调用父类的search方法, 执行默认的搜索并返回结果集
datas = super ( BlogSearchForm , self ) . search ( )
# bjy: 检查表单数据是否有效
if not self . is_valid ( ) :
# bjy: 如果表单无效, 则调用no_query_found方法( 通常返回一个空的结果集)
return self . no_query_found ( )
# bjy: 如果用户在querydata字段中输入了内容
if self . cleaned_data [ ' querydata ' ] :
# bjy: 将用户输入的搜索关键词记录到日志中
logger . info ( self . cleaned_data [ ' querydata ' ] )
# bjy: 返回搜索结果
return datas