Reputation: 31
This codes count the number of time we have visited the page, until browser is closed, using cookies. Which I am not getting. Please help
class MainPage(Handler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
visits = self.request.cookies.get('visits','0')
if visits.isdigit():
visits = int(visits) + 1
else:
visits= 0
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)
self.write("you've been here %s times" %visits)
I just want to know what is happening in these two lines
visits = self.request.cookies.get('visits','0')
and
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)
Upvotes: 0
Views: 87
Reputation: 31
Now I understand these two lines:
visits = self.request.cookies.get('visits','0')
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)
Upvotes: 0
Reputation: 16563
Rather than just give you the answer, I'll help you figure out how to get it.
self.request
and self.response
are properties of the MainPage
class. To figure out what these two things are doing you need to find out where they were defined.
The MainPage
class is a subclass of the Handler
class. You don't show the definition of the Handler
class but somewhere in your code you will find that it is a subclass of webapp2.RequestHandler
.
To find what the two lines in your code are doing, you should go read the online documentation for webapp2
.
Upvotes: 1