diff --git a/code/tango_with_django_project/rango/views.py b/code/tango_with_django_project/rango/views.py index 3eb9e03..c7ce3e2 100644 --- a/code/tango_with_django_project/rango/views.py +++ b/code/tango_with_django_project/rango/views.py @@ -13,20 +13,14 @@ from django.contrib.auth import authenticate, login # Create your views here. -def get_server_side_cookie(request, cookie, default_val=None): - val = request.session.get(cookie) - if not val: - val = default_val - return val - def visitor_cookie_handler(request): # Get the number of visits to the site. # We use the COOKIES.get() function to obtain the visits cookie. # If the cookie exists, the value returned is casted to an integer. # If the cookie doesn't exist, then the default value of 1 is used. - visits = int(get_server_side_cookie(request, 'visits', '1')) + visits = int(request.session.get('visits', '1')) - last_visit_cookie = get_server_side_cookie(request, 'last_visit', str(datetime.now()) ) + last_visit_cookie = request.session.get('last_visit', str(datetime.now())) last_visit_time = datetime.strptime(last_visit_cookie[:-7], "%Y-%m-%d %H:%M:%S") #last_visit_time = datetime.now() @@ -283,4 +277,4 @@ def profile(request, username): def list_profiles(request): # user_list = User.objects.all() userprofile_list = UserProfile.objects.all() - return render(request, 'rango/list_profiles.html', { 'userprofile_list' : userprofile_list}) \ No newline at end of file + return render(request, 'rango/list_profiles.html', { 'userprofile_list' : userprofile_list}) diff --git a/manuscript/chapter10-cookies.md b/manuscript/chapter10-cookies.md index 40b393f..7ffc8a6 100644 --- a/manuscript/chapter10-cookies.md +++ b/manuscript/chapter10-cookies.md @@ -190,19 +190,10 @@ To use the server side data, we need to refactor the code we have written so far Since all the cookies are stored server side, we won't be changing the response directly. Because of this, we can remove `response` from the `visitor_cookie_handler()` function definition. {lang="python",linenos=off} - # A helper method - def get_server_side_cookie(request, cookie, default_val=None): - val = request.session.get(cookie) - if not val: - val = default_val - return val - # Updated the function definition def visitor_cookie_handler(request): - visits = int(get_server_side_cookie(request, 'visits', '1')) - last_visit_cookie = get_server_side_cookie(request, - 'last_visit', - str(datetime.now())) + visits = int(request.session.get('visits', '1')) + last_visit_cookie = request.session.get('last_visit', str(datetime.now())) last_visit_time = datetime.strptime(last_visit_cookie[:-7], '%Y-%m-%d %H:%M:%S') @@ -289,4 +280,4 @@ X> X> - Check that your cookies are server side. Clear the browser's cache and cookies, then check to make sure you can't see the `last_visit` and `visits` variables in the browser. Note you will still see the `sessionid` cookie. Django uses this cookie to look up the session in the database where it stores all the server side cookies about that session. X> - Update the *About* page view and template telling the visitors how many times they have visited the site. Remember to call the `visitor_cookie_handler()` before you attempt to get the `visits` cookie from the `request.session` dictionary, otherwise if the cookie is not set it will raise an error. -[^1]: The latest version of the HTTP standard HTTP 1.1 actually supports the ability for multiple requests to be sent in one TCP network connection. This provides huge improvements in performance, especially over high-latency network connections (such as via a traditional dial-up modem and satellite). This is referred to as *HTTP pipelining*, and you can read more about this technique on [Wikipedia](http://en.wikipedia.org/wiki/HTTP_pipelining). \ No newline at end of file +[^1]: The latest version of the HTTP standard HTTP 1.1 actually supports the ability for multiple requests to be sent in one TCP network connection. This provides huge improvements in performance, especially over high-latency network connections (such as via a traditional dial-up modem and satellite). This is referred to as *HTTP pipelining*, and you can read more about this technique on [Wikipedia](http://en.wikipedia.org/wiki/HTTP_pipelining).