django Q模块
from django.db.models import Q def search(request): q = request.GET.get('q') if q: # 查询字段不能为None,否者报错 # title或者content中包含了搜索的关键字 newses = News.objects.filter(Q(title__icontains=q)|Q(content__icontains=q)) context = { 'newses': newses } else: context = {} return render(request,'news/search.html',context=context)