0

views我有:

        def cars(request, cars_id):
            try:
                 car = Car.objects.filter(id=cars_id).values('model')
                 all_cars = Models.objects.filter(cars_id=cars_id).values('id', 
                 'name', 'cars_id')
            except Car.DoesNotExist:
                 cars_id=1
            except Exception, e:
                 print e
            except IndexError:
                 cars_id=1
            return render_to_response('cars.html', {'all_cars': all_cars, 'car':car[0]['model']})

因此,在浏览器中我输入例如car/10000000/,我有一个错误:

IndexError: 列表索引超出范围

为什么我的“除外”不起作用以及如何解决?

4

4 回答 4

2

如果你想捕捉对象的存在,我建议你使用DoesNotExist异常

def cars(request, cars_id):
    try:
        car = Car.objects.get(id=cars_id).values('model')
    except Car.DoesNotExist:
        #other stuff

根据您更新的问题:您正在尝试获取None. 您应该在您的 try/except 语句中获取要返回的汽车模型。

于 2012-10-21T09:20:27.703 回答
1

你的问题在最后一行car[0]['model']。当您没有指定的汽车时cars_idcar列表为空,因此您将获得一个IndexError. 修复方法是在请求的汽车不存在时获取默认汽车 - 默认汽车是id = 1.

但是,您的行all_cars与您的car行相同,只是它包含额外的字段。也就是说,无效all_cars时会有一辆车或零辆车。cars_id

因此,最好all_cars列出数据库中的所有汽车:

def cars(request, cars_id):
    all_cars = Models.objects.all().values('id',
               'name', 'cars_id')
    car = Car.objects.filter(id=cars_id).values('model')
    if not car:
        car = Car.objects.filter(id=1).values('model')
    return render_to_response('cars.html', {'all_cars': all_cars,
                                            'car':car[0]['model']})
于 2012-10-21T09:56:09.910 回答
0

我猜你的 IndexError 是在其他位置抛出的。出于测试目的,尝试将整个视图包装在一个通用的 try catch 块中。

try:
    car = Car.objects.filter(id=cars_id).values('model')
    #include here all the other stuff you are doing
except Exception, e
     print e

您的观点 - 重写:

def cars(request, cars_id):
        #to see whether you passed cars_id correctly
        print "cars_id is"
        print cars_id
        all_cars = Models.objects.filter(cars_id=cars_id).values('id', 
             'name', 'cars_id')
        try:
             car = Car.objects.get(id=cars_id).values('model') 
        except Car.DoesNotExist:
             print "the car for the given ID does not exist
        #just return the "entire" car object in the context of the response.
        #in the template you can then access car.model
        return render_to_response('cars.html', {'all_cars': all_cars, 'car':car})
于 2012-10-21T09:17:45.807 回答
0

1) 了解 Exceptions hiearchy:如果except先有 Exception 块,然后有 IndexError 块,则永远不会捕获 IndexError,因为它是 Exception 的子类(参见 http://docs.python.org/library/exceptions.html#exception-hierarchy

2)filter()永远不会引发DoesNotExist异常,你必须检查是否有一些记录获取QuerySet

3)你的问题是零索引return

4) 代码应该看起来像这样:

def cars(request, cars_id):
    try:
        car = Car.objects.get(id=cars_id)
    except Car.DoesNotExist:
        #anyway - are you sure Car w/ id always exists?
        car = Car.objects.all()[:1] #get first car

    all_cars = Models.objects.filter(cars_id=cars_id).values('id', 'name', 'cars_id')
    return render_to_response('cars.html', {'all_cars': all_cars, 'car':car.model})
于 2012-10-21T10:20:03.997 回答