request.session.get('last_visit') yields None all the time
<p>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 </p>

<pre><code>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 &gt; 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)
</code></pre>

<p>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.</p>

<p>Please someone make me understand the object passed inside get().</p>

<p>Move your <code>RequestContext</code> stuff to bottom </p>

<pre><code>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 &gt; 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)
</code></pre>

python django session django-sessions