IndexError,除了不起作用。 Django的

时间:2012-10-21 08:59:28

标签: django views try-catch

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 个答案:

答案 0 :(得分: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语句中获得返回的汽车模型。

答案 1 :(得分:1)

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

但是,您的all_carscar行相同,但它包含额外的字段。换句话说,当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']})

答案 2 :(得分: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})

答案 3 :(得分:0)

1)了解异常层次结构:如果首先出现except异常块,稍后出现IndexError,则永远不会捕获IndexError,因为它是异常的子类(请参阅 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})