我一直在尋找這個答案,但沒有一個符合我的期望。因此,在我的模板中,我有一些內容,想要添加按鈕(稍后將添加到收藏夾)。單擊后,我想從 views.py 調用方法并重定向到其他視圖。我的 views.pydef home(request): //logic here request.session['url'] = url return render(request,'file.html')def function_to_call(request): ///logic here url = request.session.get('url') return render(request,'second_file.html',url=url)文件.html<form action="{% url 'function_to_call' %}"> <button id="submit" type="button" value="Click" /></form>在我的 urls.pyurl(r'^function_to_call/',views.function_to_call,name='function_to_call'),不幸的是,單擊按鈕后,沒有任何反應
2 回答

jeck貓
TA貢獻1909條經驗 獲得超7個贊
如果由于某種原因您需要使用POST請求而不是GET,這將起作用:
<form method="POST" action="{% url 'function_to_call' %}">
<button id="submit" type="submit" value="Click" />
</form>
當您不想在查詢字符串中包含數據時,使用帖子會很有幫助,因為它比在請求正文中包含參數更安全一些。
添加回答
舉報
0/150
提交
取消