471,348 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. def deleteCar(request):
  3.  
  4.     if request.user.is_authenticated():
  5.  
  6.         Login = True
  7.         myId=request.session['B_id']
  8.         my = Booking1.objects.get(id= myId)
  9.  
  10.             IF NOT EXIST my:
  11.                         return HttpResponseRedirect ("/nobooking/")
  12.         else:
  13.                         dateStr=my.booking_time
  14.                 now=datetime.datetime.now()
  15.  
  16.                 print now
  17.                 one_day=60*60*24
  18.                 diff=comp_dates(dateStr,str(now).split('.')[0])
  19.                 print diff
  20.  
  21.         if diff > one_day:
  22.             return HttpResponseRedirect ("/unable/")
  23.  
  24.         if diff < one_day:
  25.             my.delete()
  26.         else:
  27.             return HttpResponseRedirect ("/Deny/")
  28.  
  29.     return render_to_response('confirmed_cancel.html')
  30.  
the IF NOT EXIST is just my concept in the coding, can any one help me with code..thanks so much. ;)
Dec 20 '07 #1
3 18331
dazzler
75
I'm not an expert and don't know what that Booking1.objects.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)
Dec 20 '07 #2
I'm not an expert and don't know what that Booking1.objects.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.objects.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.
Dec 20 '07 #3
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> obj = None
  2. >>> print obj
  3. None
  4. >>> if not obj:
  5. ...     print "Object 'obj' evaluates to 'None'"
  6. ...     
  7. Object 'obj' evaluates to 'None'
  8. >>> obj = ""
  9. >>> if not obj:
  10. ...     print "Object 'obj' evaluates to 'None'"
  11. ...     
  12. Object 'obj' evaluates to 'None'
  13. >>> obj = []
  14. >>> if not obj:
  15. ...     print "Object 'obj' evaluates to 'None'"
  16. ...     
  17. Object 'obj' evaluates to 'None'
  18. >>> obj = {}
  19. >>> if not obj:
  20. ...     print "Object 'obj' evaluates to 'None'"
  21. ...     
  22. Object 'obj' evaluates to 'None'
  23. >>> 
This is a very basic concept.
Dec 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by sasha | last post: by
11 posts views Thread by kimiraikkonen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.