2016-10-04 72 views
0

我對網絡編程和django尤其陌生。我正在嘗試使用Ajax實現symple登錄服務。用戶似乎已成功登錄,但是當視圖發生變化時,他又重新開始篡改。Django不保留用戶在視圖之間登錄

感謝任何幫助。 謝謝。

登錄模板:

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

登錄阿賈克斯:

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 && document.cookie != '') { 
      var cookies = document.cookie.split(';'); 
      for (var i = 0; i < 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; 
    } 

Loginscript觀點:

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

else: 
    print >> sys.stderr,"Should be bad" 
    return HttpResponse("bad") 

EHEALTH觀點:

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) 

日誌我得到:

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 

因此,用戶登錄,然後重定向回到登錄頁面,未登錄

回答

2

User.is_authenticated總是真由定義,因爲你在課堂上調用它。您需要檢查實際用戶實例上的方法:在您的登錄視圖中爲user,但在索引視圖中將爲request.user

然而更簡單的方法來檢查索引視圖中的身份驗證是使用login_required decorator

+0

哦,非常感謝! – asakryukin

2

使用此代碼片段:

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) 
1

你的代碼有很多問題。

  1. 你調用從用戶類(無不是打電話,我會到達那個)is_authenticated大寫的UUser),而不是從你試圖登錄的用戶實例即user.is_authenticated

  2. is_authenticated屬性是Django版本中的一種方法< 1.10。如果您的版本低於1.10,則應該使用雙括號調用方法