2015-05-19 82 views
0

我正在處理django認證請求,我得到禁止的錯誤我檢查了我的代碼,但它似乎並沒有出現錯誤。禁止(403)CSRF驗證失敗。請求中止,當我點擊提交按鈕

HTML

<div class="grad"></div> 
    <div class="header"> 
    <div>MLAT<span>SI</span></div> 
    </div> 
    <form action="{{views.login_user}}" method="POST">{% csrf_token %} 
    <div class="login"> 
    <img src="{% static "img/Airplane.png" %}"> 
    <div id="h" class="home"> 
    <input type="text" placeholder="Login" name="username" value=""> 
    <input type="password" placeholder="Mot de passe" name="password" value=""> 
    <input style="float: left; width: 173px" type="submit" value="Log in" > 
    <input formaction="/form_registration.html/" style="float: right; width: 173px" type="submit" value="Register"> 

views.py

def login_user(request): 
    username = request.POST.get("username") 
    password = request.POST.get("password") 
    user = authenticate(username=username, password=password) 
    if user is not None and user.is_active: 
     login(request, user) 
     return HttpResponse("You're logged in.") 
    else: 
     return HttpResponse("Your username and password didn't match.") 
+1

顯示您已呈現html頁面的方法。 –

+0

「{{views.login_user}}」輸出了你期望的結果嗎?我也很好奇爲什麼你不止一次地定義表單的「action」... – rnevius

+0

你是否更改過任何默認的CSRF_ *設置? – Wtower

回答

0

看起來像一個重複:Django - {% csrf_token %} was used in a template, but the context did not provide the value

基本上,你login_user觀點是不利用任何渲染/背景,因此,該錯誤(我不知道是否與調用登錄名稱時調用的視圖相同)。所以Django會看到csrf_token,但從不將其轉換爲實際的標記值。

from django.shortcuts import render 

但是,您的表單和視圖都非常錯誤。表單操作{{views.login_user}}不正確。你不能以這種方式調用視圖。你的註冊按鈕轉到看起來像一個HTML頁面。

相關問題