request.GET [' q'],request.GET(' q')和request.GET(' q',有什么区别? ).Thanks
def search(request):
if 'q' in request.GET and request.GET['q']:
q=request.GET['q']
books=Book.objects.filter(title__icontains=q)
return render(request,'search_results.html',{'book':books,'query':q})
else:
return HttpResponse('please submit a search term')
答案 0 :(得分:1)
if 'q' in request.GET and request.GET['q']
它只检查包含q
密钥的字典。但它看起来很难看。你可以做更多pythonic:
q = request.GET.get('q') # returns None if q not in GET
if q:
do your logic