如何在try块中正确使用缩进?

This piece of code is getting me an error of invalid syntax on first return render line. I guess I have messed up my identation, but i do not know how exactly I did.

def searchView(request):
if request.method == 'GET':
    query = request.GET.get('search')
    try:
        result = Vacancy.objects.filter(name__icontains = query)
    return render(request, "search.html", {'vacancies': result })
else:
    return render(request, "search.html", {})
评论
  • <午夜m>
    <午夜m> 回复

    your try

    should have except

    做这个

    try:
       #something
    except:
       pass
    

    def searchView(request):
        if request.method == 'GET':
            query = request.GET.get('search')
            try:
                result = Vacancy.objects.filter(name__icontains = query)
            except:
                pass
            return render(request, "search.html", {'vacancies': result })
        else:
            return render(request, "search.html", {})