YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Cookies in App Engine - Web Development

Get Embed Code
2 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. What I'm going to do now is I'm going to, we're going to build a
  2. little web application that uses cookies to keep track of how many times
  3. you've visited a website. And then I'll show you, you know, how
  4. to use cookies in App Engine and some, some things we need to
  5. keep in, keep in mind, so. This is the kind of template
  6. I've been working from for App Engine stuff. First thing I want to do
  7. is, is just set the content type to text so we don't have
  8. to deal with HTML for right now. Okay, so, I've added this line
  9. here that gets a cookie called visits. The way this
  10. works is we look at the request object, which is always
  11. on self. The request object will have a cookies object, which
  12. is a convenience thing that App Engine did for us, it
  13. parsed our HTP hatters and it threw all of the
  14. cookies into this dictionary-like object and we can call get on
  15. it which you know, distinct from this get, of course. This
  16. is a function we call in dictionaries to check to see
  17. if a key is in the dictionary and if it is, we
  18. get the value of that key. And if it's not, we get the
  19. default which we'll set to zero. What I want to do is I want
  20. to print through our user how many times we visited the site. Okay,
  21. so I added this line to just write to the user you've
  22. been here %s times. Remember our
  23. string substitution? And we'll print visits there.
  24. So let's go ahead and give this a test in our browser. And
  25. I'm, I've loaded my page and it says I've been here zero times.
  26. And if I reload the page over and over, it still says zero
  27. times because we're not doing any counting, we're just printing the number zero
  28. over and over. So let's go ahead and store how many times we've
  29. been to the site in a cookie. So the way we do this
  30. first we're going to take the number of visits we have and we're going to
  31. increment it by one. Visits plus equals one and then we're going to store
  32. the number of visits in this cookie called visits so the next time
  33. we come back we'll get it. The way we set a cookie in
  34. App Engine is we just set the cookie header. Okay,
  35. so I added two more lines here. Visits plus equals 1,
  36. which will increment the visits count that we got from our
  37. cookie, and then we're going to set the Cookie. So the way
  38. we do that is on the response object, there is
  39. a headers object. You know, just like when we set the
  40. Content Type. And now we want to, we're going to call add_header. We
  41. could treat it like a dictionary, but add_header here is special
  42. because remember, you can have multiple headers with the same name,
  43. and so if you used a dictionary syntax, you'd overwrite the header.
  44. So in this case, we're going to use add_header, that's what we're going to
  45. use for cookies here. So we're going to use the Set-Cookie header, and
  46. I'm just going to use, I'm not going to worry about domain and path
  47. right now. That's not what we're trying to experiment with here and
  48. we're going to say, visits equals %s, and we're going to include our Visits
  49. parameter. All right, so let's see if that works. We're on our,
  50. our browser and you know when I, and I've
  51. reloaded the page. And so we see I've been
  52. here one time, and I reload the page again.
  53. Ooh, and we see an error, and this makes sense.
  54. Need a string, int found on visits plus equals
  55. 1. Cookies you know, the browser doesn't care what datatype
  56. our cookie is here we just says that it's,
  57. you know, it's basically this, this, this string, this blob
  58. of data and here I am treating it like an
  59. integer. So let's, let's go ahead and update this code to
  60. make this work properly for strings. Okay, so, we've improved
  61. our code a little bit here. I changed our, our default
  62. zero to the string zero and so that this next
  63. line will work without exceptions. If visits.isdigit, remember that checks to
  64. see if visits is made up of only, only digits in
  65. which case we can safely convert it to an integer and
  66. we add one to it. So we increment visits every time,
  67. otherwise we just set visits to zero and that's what we print.
  68. Okay, so let's give this a whirl. So now we've got our
  69. page here, and I re-load it, the arrow went away, and we
  70. can see this counting up. Pretty neat huh, so that count is
  71. stored in a cookie in this browser. The server has no idea
  72. what the count is, so, let's go ahead and add some functionality
  73. to further my agenda. Now what we want to do, is, is let's,
  74. let's do something with this cookie value. Let's say we want to reward users who
  75. have been to our website 100 times with a special message of thanks. So
  76. let's, let's do it like let's, let's make some changes here. We'll say if
  77. visits is greater than 100, self.write you are
  78. the best ever. And for everybody else, we'll just
  79. tell them how many times they've been here. Let's see this in action.
  80. Actually so we want to, we want to do it on 100, but let's go ahead
  81. and, and test it on ten so we don't have to reload the
  82. page 100 times, just to make sure this works. Okay, so I'm going to save
  83. this, and we're going to go to our browser and give it a shot.
  84. Conveniently I'm at nine times we, reload ten times. Hah greater than ten after
  85. I reloaded it switched to you are the best ever. Great, we feel very
  86. good let's go back to our code and change that ten to a 100.
  87. So change that 10 to a 100, we go back to
  88. our browser, we keep reloading again. Okay, so we really want to see
  89. our, our special message. But it's going to take us awhile to
  90. get there, we're going to have to sit here and reload the page.
  91. Actually let's make this 100,000, okay? We're never going to get there,
  92. we're going to have to be a super loyal user to, to get
  93. up to that amount. So, what I'd like you to do
  94. is think about, how we might cheat the system a little bit.