Django doesn't keep User logged in between views
<p>I am quite new to web programming and django especially. I am trying to implement symple login service using Ajax. The user seems to be logged in succesfully however when the view is changed he uppears ulogged again. </p>

<p>Appreciate any help.
Thanks.</p>

<p>Login template:</p>

<pre><code>&lt;form class="login-form" action=""&gt;
    {% csrf_token %}
  &lt;input type="text" id="usernamelog" /&gt;
  &lt;input type="password" id="pwdlogin" /&gt;
  &lt;button onclick="login(event)"&gt;login&lt;/button&gt;
  &lt;p class="message"&gt;Not registered? &lt;a href="#"&gt;Create an account&lt;/a&gt;&lt;/p&gt;
&lt;/form&gt;
</code></pre>

<p>Login Ajax:</p>

<pre><code>function login(e) {
        e.preventDefault();
        var username = $("#usernamelog").val();
        var pwd = $("#pwdlogin").val();
        $.ajaxSetup({
            beforeSend: function(xhr, settings) {
                if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                    // Only send the token to relative URLs i.e. locally.
                    xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
                }
            }
        });
        $.ajax({
            url : "/loginscript/",
            type : "post",
            data : {
                username: username,
                password : pwd,
            }
        }).done(function(data) {
            if (data == "good") {
                document.getElementById('usernamelog').value ="good";
                window.location='../ehealth'
            }else{
                document.getElementById('usernamelog').value ="bad";
            }
        });
    }


    function getCookie(name) {
         var cookieValue = null;
         if (document.cookie &amp;&amp; document.cookie != '') {
             var cookies = document.cookie.split(';');
             for (var i = 0; i &lt; cookies.length; i++) {
                 var cookie = jQuery.trim(cookies[i]);
                 // Does this cookie string begin with the name we want?
                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                     break;
                 }
             }
         }
         return cookieValue;
     }
</code></pre>

<p>Loginscript view:</p>

<pre><code>def loginscript(request):
#c = {}
#c.update(csrf(request))
print &gt;&gt; sys.stderr,"script entered"
username = request.POST['username']
password = request.POST['password']
print &gt;&gt; sys.stderr, username
user = authenticate(username=username, password=password)
if user is not None:
    login(request=request,user=user)
    if User.is_authenticated:
        print &gt;&gt; sys.stderr,"should be good actually"
    else:
        print &gt;&gt; sys.stderr, "Still not"
    return HttpResponse("good")

else:
    print &gt;&gt; sys.stderr,"Should be bad"
    return HttpResponse("bad")
</code></pre>

<p>Ehealth view:</p>

<pre><code>def index(request):
check=User.is_authenticated
if check!=True:
    return redirect('http://127.0.0.1:8000/login/')

template="index.html"
return render (request=request, template_name=template)
</code></pre>

<p>The log I get: </p>

<pre><code>Hey we are in login
[04/Oct/2016 14:02:42] "GET /login/ HTTP/1.1" 200 6881
script entered
Andrey
should be good actually
[04/Oct/2016 14:02:46] "POST /loginscript/ HTTP/1.1" 200 4
[04/Oct/2016 14:02:46] "GET /ehealth/ HTTP/1.1" 302 0
Hey we are in login
</code></pre>

<p>So the user is logged in and then redirected back to login page as unlogged</p>

<p>Use this code snippet:</p>

<pre><code>    def index(request):          
      if not request.user.is_authenticated():
        return redirect('http://127.0.0.1:8000/login/')

      template="index.html"
      return render (request=request, template_name=template)
</code></pre>

python ajax django csrf