views:

286

answers:

2

This view works when the query is in the database.

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True)
        q_year = Lawyer.objects.filter(last__icontains=q).values_list('year_graduated', flat=True)
        lawyers = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year[0]).exclude(last__icontains=q)        
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')

So, if q=delelle then it finds other lawyers in the database who graduated from the same school the same year.

if q=collins and collins is in the database but there are no other lawyers who graduated from the same school the same year then it gives appropriate error message "No lawyers matched your search criteria."

But if q=moritz and there is no lawyer named moritz in the database then it gives a 500 internal server error.

I don't understand the request.GET['q'] notation or how I can fix this so that I can add the proper text for the case when the query is not in the database. Can you please point me in the right direction? Thanks.

Edit re Antony Hatchkins answer

The below code works without giving the error. I will try to incorporate the rest soon:

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        lawyer = Lawyer.objects.filter(last__icontains=q)
        if len(lawyer)==0:
            return render_to_response('not_in_database.html', {'query': q})
        else:
            q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True)
            q_year = Lawyer.objects.filter(last__icontains=q).values_list('year_graduated', flat=True)
            lawyers = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year[0]).exclude(last__icontains=q)           
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')
+1  A: 
def search(request):
    q = request.GET.get('q', '')
    if q:
        lawyers = Lawyer.objects.filter(last__icontains=q)
        if len(lawyers)==0:
            return HttpResponse('No such lawyer')
        if len(lawyers)>1:
            return HttpResponse('Several lawyers matched')
        lawyers1 = Lawyer.objects.filter(school=lawyers[0].school).filter(year_graduated=lawyers[0].year).exclude(pk=lawyers[0].pk)        
        return render_to_response('search_results.html', {'lawyers': lawyers1, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')

request.GET['q'] fetches field q from client browser's GET request object

Antony Hatchkins
Your original function would take random item from the database if several items have matched which doubtfully is what you expect. This code will inform about the issue
Antony Hatchkins
Thanks. I didn't realize this. But I think, you edited your initial answer. At first I couldn't make it work, then I changed a little bit, now it is working. I created a new template not_in_database.html and this will handle it. I will post the code as an edit soon. I would appreciate comments. Thanks.
Zeynel
Welcome. Yes I fixed a typo or two.
Antony Hatchkins
+1  A: 

This should do the trick (not tested):

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        try:
            q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True)
            q_year = Lawyer.objects.filter(last__icontains=q).values_list('year_graduated', flat=True)
            lawyers = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year[0]).exclude(last__icontains=q)        
        except Lawyer.DoesNotExist:
            lawyers = []
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')
jbochi
In my first try I got the 500 error again. I may be doing something wrong.
Zeynel