2016-09-04 96 views
-1

request.GET ['q'], request.GET中( 'q'),和request.GET中( 'q')。謝謝request.GET ['q'],request.GET('q')和request.GET('q',)

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