title: Using manual login in django
body: <p>I am following the <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.login" rel="nofollow">sample code</a> on the django docs for the <code>auth</code> function <code>login()</code> for the purpose of learning about the various ways login can be achieved. I am aware of the <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#module-django.contrib.auth.forms" rel="nofollow">built-in forms</a> and <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#module-django.contrib.auth.views" rel="nofollow">built-in views</a>, and will ultimately end up using them, but I am trying to <em>understand</em>, not just to know.</p>
  
  <p>The problem I'm having is that the user is logged in successfully in check_user, but doesn't seem to persist in the session when the redirect is called. Why is the is_authenticated() function giving me inconsistent results? I have a feeling it's to do with RequestContext, but I'm not sure what.</p>
  
  <pre><code>def check_user(request):
      username = request.POST['username']
      password = request.POST['password']
      user = authenticate(username=username, password=password)
      if user is not None:
          if user.is_active:
              login(request, user)
              test1 = user.is_authenticated() ### True
              test2 = request.user.is_authenticated() ### False
              return HttpResponseRedirect(NEXT_PAGE) ### NEXT_PAGE gives request.user.is_authenticated() is False
          else:
              return HttpResponse("disabled account")
      else:
          return HttpResponse("invalid login")
  </code></pre>