0

I am recently into session and cookies. I comprehend session and cookies well in theory but i have one issue in understanding the code of session. It is about getting the last_visit by the user. The code is from the tangowithdjango.com

def index(request):
    context = RequestContext(request)

    category_list = Category.objects.all()
    context_dict = {'categories': category_list}

    for category in category_list:
        category.url = encode_url(category.name)

    page_list = Page.objects.order_by('-views')[:5]
    context_dict['pages'] = page_list

    #### NEW CODE ####
    if request.session.get('last_visit'):
        # The session has a value for the last visit
        last_visit_time = request.session.get('last_visit')
        visits = request.session.get('visits', 0)

        if (datetime.now() - datetime.strptime(last_visit_time[:-7], "%Y-%m-%d %H:%M:%S")).days > 0:
            request.session['visits'] = visits + 1
            request.session['last_visit'] = str(datetime.now())
    else:
        # The get returns None, and the session does not have a value for the last visit.
        request.session['last_visit'] = str(datetime.now())
        request.session['visits'] = 1
    #### END NEW CODE ####

    # Render and return the rendered response back to the user.
    return render_to_response('rango/index.html', context_dict, context)

When i tried to understand what request.session.get('last_visit') does, i get None all the time. What i don't understand is the key 'last_visit'. Is it default object inside session? If it is default in the session object then why it shows None every time in my terminal.

Please someone make me understand the object passed inside get().

pythonBeginner
  • 781
  • 2
  • 12
  • 27

1 Answers1

0

Move your RequestContext stuff to bottom

def index(request):


    #### NEW CODE ####
    if request.session.get('last_visit'):
        # The session has a value for the last visit
        last_visit_time = request.session.get('last_visit')
        visits = request.session.get('visits', 0)

        if (datetime.now() - datetime.strptime(last_visit_time[:-7], "%Y-%m-%d %H:%M:%S")).days > 0:
            request.session['visits'] = visits + 1
            request.session['last_visit'] = str(datetime.now())
    else:
        # The get returns None, and the session does not have a value for the last visit.
        request.session['last_visit'] = str(datetime.now())
        request.session['visits'] = 1
    #### END NEW CODE ####

    context = RequestContext(request)

    category_list = Category.objects.all()
    context_dict = {'categories': category_list}

    for category in category_list:
        category.url = encode_url(category.name)

    page_list = Page.objects.order_by('-views')[:5]
    context_dict['pages'] = page_list

    # Render and return the rendered response back to the user.
    return render_to_response('rango/index.html', context_dict, context)
levi
  • 22,001
  • 7
  • 73
  • 74
  • http://www.tangowithdjango.com/book/chapters/cookie.html . Here RequestContext is at the top. My main question is what is 'last_visit'. Is it buit in object inside sessions object ? How to see what is inside request.session? – pythonBeginner Aug 31 '16 at 04:21
  • @pythonLover print `request.session.keys()` – levi Aug 31 '16 at 04:22
  • i get only these 3 session dict_keys(['_auth_user_backend', '_auth_user_hash', '_auth_user_id']). From where that 'last_visit' has come to request.session? – pythonBeginner Aug 31 '16 at 04:28
  • plz post your settings. – levi Aug 31 '16 at 04:30
  • https://github.com/leifos/tango_with_django_17/blob/master/tango_with_django_project_17/settings.py – pythonBeginner Aug 31 '16 at 04:39
  • aforementioned i am learning from the tangowithdjango about session. You can see the code there and its a working code. Thanks – pythonBeginner Aug 31 '16 at 04:40
  • Looks like its using the default session backend that uses the database to storing session data. Can you check whether your database has the table 'django_session'. If not, run python manage.py migrate to create it. – Abhinav Aug 31 '16 at 07:08