如何捕获Django request.GET错误? [英] How to catch Django request.GET error?
问题描述
def search(request):
if' q'请求.GET和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)
律师= 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})
否则:
返回HttpResponse('请提交搜索字词')
所以,如果 q = delelle ,那么它会发现在同一所学校毕业的数据库中的其他律师是一样的年。
如果 q = collins 和collins在数据库中,但没有其他律师毕业同一个学校在同一年,那么它给出适当的错误信息没有律师符合您的搜索条件。
但如果 q = moritz ,在数据库中没有名为 moritz
的律师,内部服务器错误。
我不明白 request.GET ['q']
符号或我如何可以修复这个,以便我可以在查询不在数据库中的情况下添加适当的文本。你能指出我正确的方向吗?谢谢。
编辑re Antony Hatchkins答案
错误。我将尝试尽快纳入其中:
def search(request):
if'q'in request。 GET和request.GET ['q']:
q = request.GET ['q']
律师= Lawyer.objects.filter(last__icontains = q)
如果len(律师)= = 0:
return render_to_response('not_in_database.html',{'query':q})
else:
q_school = Lawyer.objects.filter(last__icontains = q).values_list('学校',flat = True)
q_year = Lawyer.objects.filter(last__icontains = q).values_list('year_graduated',flat = True)
律师= 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('请提交搜索字词')
def search(request):
q = request.GET.get('q','')
if q:
律师= Lawyer.objects.filter(last__icontains = q)
如果len(律师)== 0:
返回HttpResponse('没有这样的律师')
如果len (律师)> 1:
返回HttpResponse('几个律师匹配')
律师1 =律师.objects.filter(学校=律师[0] .school).filter(year_graduated =律师[0] 。年份).exclude(pk = lawyers [0] .pk)
return render_to_response('search_results.html',{'lawyers':lawyers1,'query':q})
else:
return HttpResponse('请提交搜索字词')
请求.GET ['q']
从客户端浏览器的 GET
请求对象中提取字段 q
/ p>
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.')
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
这篇关于如何捕获Django request.GET错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!