1
我試圖實現一個登錄系統,用戶永遠不會離開首頁。到目前爲止,我有以下幾點:django登錄索引頁
形式:
<form action="" method="post">
{% csrf_token %}
<input type="text" name="username" value="" id="username">
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next|escape }}" />
</form>
的觀點:
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('index.html',{'state':state, 'username': username})
當我提交表單與已知用戶,我得到的錯誤:
CSRF令牌丟失或不正確。
我在做什麼錯?我已經得到表單中的令牌。
我將如何使用context_instance?渲染(請求,'index.html',[這裏發生了什麼?]) – babbaggeii
哦,context_instance用於render_to_response。它是自動渲染的(注意渲染將請求作爲參數,允許context_processors運行)。它有竅門嗎? – jpic
我仍然收到相同的錯誤消息。 return語句現在是:return render(request,'index.html'),我也嘗試了我的前一個上下文:return render(request,'index.html',{'state':state,'username':用戶名}) – babbaggeii