request.GET [' q'],request.GET(' q')和request.GET(' q',)之间有什么区别?

时间:2016-09-04 16:32:41

标签: python django

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')

1 个答案:

答案 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