how to Check if the id is exist in my database?

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

    how to Check if the id is exist in my database?

    hi

    currently i am doing car booking website. once customer booked car with us, we will give customer the booking id, thus if they would like to update or cancel, they need to type in their respective booking id. currently i am looping through all the id in my Booking1 table and check if there is any id match with the id post over from user. my code of update is in the following:

    [code=python]
    def confirmUp(reque st):
    Array=[]
    q=request.sessi on['BK_id']
    uID=request.use r.id
    print uID


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

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

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

    but it seems never go through if statement. and i have print value of p and the id in my Booking1 table. it is the same value and i am wondering why they do not go into my if statement, i believe my equal operator"==" should be used correct. can anyone help me with this. thanks in advance :)

    it is a bit urgent at the moment, as project timeline is running out. thanks :)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by kang jia
    hi

    currently i am doing car booking website. once customer booked car with us, we will give customer the booking id, thus if they would like to update or cancel, they need to type in their respective booking id. currently i am looping through all the id in my Booking1 table and check if there is any id match with the id post over from user. my code of update is in the following:

    [code=python]
    def confirmUp(reque st):
    Array=[]
    q=request.sessi on['BK_id']
    uID=request.use r.id
    print uID


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

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

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

    but it seems never go through if statement. and i have print value of p and the id in my Booking1 table. it is the same value and i am wondering why they do not go into my if statement, i believe my equal operator"==" should be used correct. can anyone help me with this. thanks in advance :)

    it is a bit urgent at the moment, as project timeline is running out. thanks :)
    When you print an object, the object is converted to a string. The objects may print the same but be different types. Example:[code=Python]>>> a = '123456'
    >>> b = 123456
    >>> print a
    123456
    >>> print b
    123456
    >>> [/code]It is possible that the objects i.id and q are different types. Try this:[code=Python]if str(i.id) == str(q):[/code]

    Comment

    • kang jia
      New Member
      • Jun 2007
      • 88

      #3
      Originally posted by bvdet
      When you print an object, the object is converted to a string. The objects may print the same but be different types. Example:[code=Python]>>> a = '123456'
      >>> b = 123456
      >>> print a
      123456
      >>> print b
      123456
      >>> [/code]It is possible that the objects i.id and q are different types. Try this:[code=Python]if str(i.id) == str(q):[/code]
      You are really expert, it really works by adding in str (), but where can i know what is their data type before using str() to make them belong to the same data type. in fact, if i know this, i can better prevent it from happening again in the future programming. haha, thanks so much :) i am so happy it works now! yeah!!

      Comment

      • kang jia
        New Member
        • Jun 2007
        • 88

        #4
        Originally posted by kang jia
        You are really expert, it really works by adding in str (), but where can i know what is their data type before using str() to make them belong to the same data type. in fact, if i know this, i can better prevent it from happening again in the future programming. haha, thanks so much :) i am so happy it works now! yeah!!
        sorry, when i restart the server, it pop me the error message, i am not sure where is wrong, just now still able to work.

        my error message is
        Exception Value: The view fyp2.car.views. confirmUp didn't return an HttpResponse object.

        [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 str(i.id) ==str(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]

        can help me with this and i would it to work properly, thanks so much :)

        Comment

        • kang jia
          New Member
          • Jun 2007
          • 88

          #5
          Originally posted by kang jia
          You are really expert, it really works by adding in str (), but where can i know what is their data type before using str() to make them belong to the same data type. in fact, if i know this, i can better prevent it from happening again in the future programming. haha, thanks so much :) i am so happy it works now! yeah!!
          sorry, when i restart the server, it pop me the error message, i am not sure where is wrong, just now still able to work.

          my error message is
          Request Method: GET
          Request URL: http://localhost:8000/proceed/
          Exception Type: ValueError
          Exception Value: The view fyp2.car.views. confirmUp didn't return an HttpResponse object.

          [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 str(i.id) ==str(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]

          can help me with this and i would it to work properly, thanks so much :)

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by kang jia
            sorry, when i restart the server, it pop me the error message, i am not sure where is wrong, just now still able to work.

            my error message is
            Request Method: GET
            Request URL: http://localhost:8000/proceed/
            Exception Type: ValueError
            Exception Value: The view fyp2.car.views. confirmUp didn't return an HttpResponse object.

            [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 str(i.id) ==str(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]

            can help me with this and i would it to work properly, thanks so much :)
            I am not familiar with that error. You can check data types with the type() and isinstance() functions.[code=Python]>>> onebefore
            1198191905.0
            >>> str(onebefore)
            '1198191905.0'
            >>> type(onebefore)
            <type 'float'>
            >>> type(str(onebef ore))
            <type 'str'>
            >>> type(str(onebef ore)) == type('abc')
            True
            >>> p1=Point(1,2,3)
            >>> type(p1)
            <class '__main__.Point '>
            >>> isinstance(p1, Point)
            True
            >>> isinstance('abc ', str)
            True
            >>> isinstance(123. 45, float)
            True
            >>> isinstance({}, dict)
            True
            >>> [/code]

            Comment

            Working...