我不知道如何解決此CSRF故障。我已經包含了我一直在閱讀的{%csrf_token%},但它仍然給我這個錯誤。下面是代碼:CSRF驗證失敗 - Django
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Log in</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>
body{
font-family:Arial,Helvetica,sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
{{ state }}
<form action="/login/" method="post">{% csrf_token %}
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
username:
<input type="text" name="username" value="{{ username}}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In" />
</form>
</body>
</html>
我已經包括所有適當的功能,這個,但如果你需要看到更多的代碼,讓我知道......我想這是所有應用雖然...
編輯:views.py
from django.shortcuts import render_to_response
from django.contrib.auth import *
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response('auth.html',{'state':state, 'username': username})
是什麼 「這個錯誤」?你能發佈你所看到的錯誤的完整追溯嗎?你能發佈視圖代碼嗎?它會幫助我們更好地診斷您的問題。 – sdolan 2013-03-08 22:45:01
你是否只包含沒有HTML標記的標記? – user2398029 2013-03-08 22:46:54
^^^它應該被包括在內。 OP:你是否檢查了HTML以查看'{%csrf_token%}'標籤實際上是否生成了隱藏的輸入元素?否則請複習點1.至3.這裏:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ – Tobia 2013-03-08 22:47:51