3 回答

TA貢獻1864條經驗 獲得超2個贊
您的代碼很好,唯一的問題是,next因為方法是,所以您以表單的形式將屬性傳遞為帖子post。在視圖中,您嘗試next在get字典中獲取顯然不存在的參數。
您必須action像這樣聲明html表單,以便您的視圖工作。
{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
{% csrf_token %}
username:
<input type="text" name="username" value="{{ username }}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In"/>
{% debug %}
</form>
在那里,如果有一個next變量,則將其包含在其中url以作為獲取參數來進行檢索。如果沒有,則表單不包含它。
這是最好的方法,但是您也可以通過next從POST字典中請求這樣來在視圖中解決此問題:
return HttpResponseRedirect(request.POST.get('next'))
請注意,這僅在模板account_login 具有名為的變量時才有效next。您應該在視圖中生成它,并在渲染它時將其傳遞給模板。
通常,在模板中,您將執行以下操作:
# this would be hardcoded
next = '/issueapp/1628/view/22'
# you may add some logic to generate it as you need.
然后您執行以下操作:
return render_to_response(
'account_login.html',
{
'state':state,
'username': username,
'next':next
},
context_instance=RequestContext(request)
)
希望這可以幫助!

TA貢獻1821條經驗 獲得超6個贊
如果您想變得更通用,則可以執行以下操作,在發布表單時,它會傳遞任何GET參數:
<form action="/path-to-whatever/{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}" method="post">
添加回答
舉報