how to express " if Not exist" concept in python

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

    #1

    how to express " if Not exist" concept in python

    hi, currently i am doing booking car online and one of the function is cancel and update booking. if the booking number users entered in is not exist. it will redirect the page stating that the booking number did not exist or you never book with us. however. i would like to express this "if Not exist" concept in my coding. how should i achieve this. the following is my code:

    [code=python]

    def deleteCar(reque st):

    if request.user.is _authenticated( ):

    Login = True
    myId=request.se ssion['B_id']
    my = Booking1.object s.get(id= myId)

    IF NOT EXIST my:
    return HttpResponseRed irect ("/nobooking/")
    else:
    dateStr=my.book ing_time
    now=datetime.da tetime.now()

    print now
    one_day=60*60*2 4
    diff=comp_dates (dateStr,str(no w).split('.')[0])
    print diff

    if diff > one_day:
    return HttpResponseRed irect ("/unable/")

    if diff < one_day:
    my.delete()
    else:
    return HttpResponseRed irect ("/Deny/")

    return render_to_respo nse('confirmed_ cancel.html')
    [/code]

    the IF NOT EXIST is just my concept in the coding, can any one help me with code..thanks so much. ;)
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    I'm not an expert and don't know what that Booking1.object s.get(id= myId) do , but can you search it first and check if it exist? In my code I have used sometimes try, expect in some places, it raises error if something doesn't exist (if for example trying to modify some variable that doesn't exist)

    Comment

    • kang jia
      New Member
      • Jun 2007
      • 88

      #3
      Originally posted by dazzler
      I'm not an expert and don't know what that Booking1.object s.get(id= myId) do , but can you search it first and check if it exist? In my code I have used sometimes try, expect in some places, it raises error if something doesn't exist (if for example trying to modify some variable that doesn't exist)
      i am using Django framework, Booking1.object s.get(id= myId) what this code do is the same as SQL select statement where id is equal to myId which is post over from previous page. yes, i have tried this if not exist phrase before i post this question in forum and it returns me invalid syntax. thanks for your kind reply as well.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        [code=Python]>>> obj = None
        >>> print obj
        None
        >>> if not obj:
        ... print "Object 'obj' evaluates to 'None'"
        ...
        Object 'obj' evaluates to 'None'
        >>> obj = ""
        >>> if not obj:
        ... print "Object 'obj' evaluates to 'None'"
        ...
        Object 'obj' evaluates to 'None'
        >>> obj = []
        >>> if not obj:
        ... print "Object 'obj' evaluates to 'None'"
        ...
        Object 'obj' evaluates to 'None'
        >>> obj = {}
        >>> if not obj:
        ... print "Object 'obj' evaluates to 'None'"
        ...
        Object 'obj' evaluates to 'None'
        >>> [/code]This is a very basic concept.

        Comment

        Working...