UnboundLocalError:赋值前引用本地变量'prod_Available' [英] UnboundLocalError: local variable 'prod_Available' referenced before assignment

查看:337
本文介绍了UnboundLocalError:赋值前引用本地变量'prod_Available'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个预订系统,而且我有一个保存产品数量的功能...我的问题是为什么我有这个问题?当我

I am developing a reservation system, and i have a function that save the quantity of a product... My question is why I got this problem ? when i

`curl -l -X POST -d "product=3&client=1&function=insert_booking&check_in=2011-12-15&check_out=2011-12-10&no_of_adult=2&no_of_kid=1&quantity=2&first_name=asda&last_name=sdsd&contact=34343" http://127.0.0.1:8000/api/reserve`


Piston/0.3dev (Django 1.3.1) crash report:

Traceback (most recent call last):

  File "/home/agileone/workspace/bookproj/api/handlers.py", line 206, in create
    prodAvailable = Hotel.objects.get_hotel_sum_quantity(attrs['product'], attrs['check_in'], attrs['check_out'])

  File "/home/agileone/workspace/bookproj/../bookproj/booking/models.py", line 49, in get_hotel_sum_quantity
    if prod_Available <= 0:

UnboundLocalError: local variable 'prod_Available' referenced before assignment

但是当我在python shell中测试时,它可以正常工作:

but when i test in the python shell, it works fine :

>>> from booking.models import *
>>> Hotel.objects.get_hotel_sum_quantity(3, '2011-12-10', '2011-12-15')1

这里是我的代码在models.py

here is my code in models.py

def get_hotel_sum_quantity(self, product_id, checkin_date, checkout_date):
        check_in = datetime.datetime.strptime(checkin_date, '%Y-%m-%d')
        check_in = check_in.date()
        start_date = check_in.day

        check_out = datetime.datetime.strptime(checkout_date, '%Y-%m-%d')
        check_out = check_out.date()
        end_date = check_out.day

        prod = Product.objects.get(id=product_id)

        for x in range(start_date,end_date + 1):
            x = x - start_date
            delta = datetime.timedelta(days=x)
            all_date = check_in + delta
            sumOfQuantity = HotelCheck.objects.filter(date_booked=all_date, product=prod).aggregate(Sum('quantity'))['quantity__sum']
            if sumOfQuantity == None:
                sumOfQuantity = 0
            prod_Available = prod.quantity - sumOfQuantity
            #global prod_Available
        if prod_Available <= 0:
            status = 0
        else:
            status = 1

        return status

和我的handlers.py

and my handlers.py

if attrs['function'] == 'insert_booking':

                prodAvailable = Hotel.objects.get_hotel_sum_quantity(attrs['product'], attrs['check_in'], attrs['check_out'])
                if float(prodAvailable) <= 0:
                    disp = Hotel.objects.get_hotel_show_available(attrs['product'], attrs['check_in'], attrs['check_out'])
                    return {'status': '0', 'message': 'not OK!'}, disp    

任何人都可以解释我的情况,并给出一些想法如何解决它?
谢谢

can anyone can explain my situation and give some idea on how to solve it...? thanks

推荐答案

有时你没有输入循环,所以 prod_Available 不是创建的,但是您尝试引用它。

Sometimes you loop is not entered, so prod_Available is not created, but you try to reference it.

循环之前, prod_Available = 0

    prod = Product.objects.get(id=product_id)

    prod_Available = 0 # !

    for x in range(start_date,end_date + 1):
        x = x - start_date
        delta = datetime.timedelta(days=x)
        all_date = check_in + delta
        sumOfQuantity = HotelCheck.objects.filter(date_booked=all_date, product=prod).aggregate(Sum('quantity'))['quantity__sum']
        if sumOfQuantity == None:
            sumOfQuantity = 0
        prod_Available = prod.quantity - sumOfQuantity
        #global prod_Available
    if prod_Available <= 0:
        status = 0
    else:
        status = 1

    return status

这篇关于UnboundLocalError:赋值前引用本地变量'prod_Available'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆