how to correctly nest if statement into for loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kang jia
    New Member
    • Jun 2007
    • 88

    how to correctly nest if statement into for loop?

    hi, currently i am doing a car rental website, once it is bigger tthan 1 day's time which is 24 hours, its inventory will be updated and only admin can executed this. However if it is smaller than 24 hours, then it will still remain at the current inventory. the car's booking time is inside my Booking1 table and car_inventory is inside CarModel table.
    [code=python]
    def inventory(reque st):

    allObj=[]
    for i in Booking1.object s.all():
    #print i.booking_time
    invtime=i.booki ng_time
    t=str(invtime). split('.')[0]
    now=datetime.da tetime.now()
    one_day=60*60*2 4
    diff=comp_dates (t,str(now).spl it('.')[0])
    print diff
    if diff>one_day:
    for a in CarModel.object s.all():
    qtyInStock=a.ca r_inventory
    orderQuantity = 1
    qtyLeft = int(qtyInStock) - int(orderQuanti ty)
    a.car_inventory =qtyLeft
    a.save()
    allObj=CarModel .objects.all()
    print allObj
    print "LLLLLLLLLLLLLL LLLL"
    return render_to_respo nse("inventoryu pdate.html", {'allObj':allOb j})
    if not diff>one_day:
    return render_to_respo nse("inventory. html")
    [/code]

    as for this code, it don not error but i never go through my if not condition even through it fulfills this condition. can anyone help me with this, thus it will go through this "if not diff>one_day:" condition.thank s so much for kind help :)
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    have you tried using if diff<one_day: ? :D

    or what about:[code=python]
    if diff>one_day:
    #something
    else:
    #something[/code]

    Comment

    • dazzler
      New Member
      • Nov 2007
      • 75

      #3
      hmmh, should this "return render_to_respo nse("inventoryu pdate.html", {'allObj':allOb j})" be inside of your first if statement? because now it isn't

      Comment

      • kang jia
        New Member
        • Jun 2007
        • 88

        #4
        Originally posted by dazzler
        hmmh, should this "return render_to_respo nse("inventoryu pdate.html", {'allObj':allOb j})" be inside of your first if statement? because now it isn't

        thanks for your kind help, in that scenario it works according to your way, but one thing i do not understand is that why everytime it will go to updateinventory page even the content has changed to inventory page.it seems quite strange to me. i mean how the updateinventory page knows inventory pages' content??Althou gh it works in the way i want yet i do not understand its logic..hmm, if possible, can explain this to me, thanks so much.:)

        however, i would like to ask you about one more scenario.in fact, it is the same logic but did not work in the other part of my coding. the coding is in the following:

        [code=python]
        def confirmUp(reque st):
        Array=[]
        ba={}
        q=request.sessi on['BK_id']
        uID=request.use r.id
        #ba=Booking1.ob jects.get(id=q)
        #print ba.id

        for i in Booking1.object s.all():

        if i.id == q:
        print "PPPPPPPPP"
        ba=Booking1.obj ects.get(id=q)
        print "LLLLLLLLLLLLLL LL"
        Array.append(ba )
        return render_to_respo nse('proceed.ht ml',{'Array': Array })

        else:
        return HttpResponseRed irect("/Notexist/")
        [/code]

        it seems never go into if statement even the condition is fulfilled at "if i.id == q:"
        thanks for your kind help :)

        Comment

        Working...