如何捕获Django request.GET错误?

时间:2010-01-14 15:57:14

标签: django django-forms

当查询在数据库中时,此视图有效。

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

所以,如果q=delelle,那么它会在数据库中找到同一年毕业于同一所学校的其他律师。

如果q=collins和collins在数据库中,但同年没有其他律师从同一所学校毕业,则会给出相应的错误消息“没有律师符合您的搜索条件。”

但如果q=moritz并且数据库中没有名为moritz的律师,则会产生500内部服务器错误。

我不理解request.GET['q']符号或如何解决这个问题,以便在查询不在数据库中时可以为案例添加正确的文本。你能指点我正确的方向吗?感谢。

编辑re Antony Hatchkins回答

以下代码无需提供错误即可运行。我将尽快将其余部分纳入其中:

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

2 个答案:

答案 0 :(得分:2)

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']从客户端浏览器的q请求对象中提取字段GET

答案 1 :(得分:1)

这应该可以解决问题(未经测试):

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