我在Django創建我的第一個應用程序 我決定我想在每個視圖上記錄窗口(小一個用戶名/密碼爲「登錄」按鈕)。 從base.html文件:Django登錄後重定向
<div id="logging">
{% if user.is_authenticated %}
<h1>Witaj, {{user.username}}!</h1>
{% else %}
<form method="post" action="{% url 'harcowanie.views.login' %}">
{% csrf_token %}
<div style="display: inline-block;">
<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="30" /></p>
<p><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></p>
</div>
<div style="display: inline-block; position: relative; bottom: 25px;">
<input class="decent_background" type="submit" value="Log in" />
</div>
</form>
{% endif %}
</div>
我views.py的任何部分:
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request=request, user=user)
return redirect(request.META['HTTP_REFERER'], context_instance=RequestContext(request))
else:
pass
# Return a 'disabled account' error message
else:
pass
# Return an 'invalid login' error message.
return redirect(request.META['HTTP_REFERER'], context_instance=RequestContext(request))
但我發現了錯誤:就像你在錯誤的方式使用redirect
A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext. warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.")
感謝它似乎解決了我的問題。 – 2014-09-21 18:57:17