【发布时间】:2017-09-20 10:54:14
【问题描述】:
在django 1.10中,我需要将参数从一个html模板传回view.py,最后再次渲染相同的视图。
问题在于在 html 中设置 action 以使用 javascript。
这是我的 html 模板:
<form method='POST' action='/res/{{link}}/{{page_id}}/{{email}}/'>
<input type="hidden" name="email" value="{{email}}">
<input type="image" type='submit' src={% static "assets/images/twitter_icon.png" %}>
</form>
action 中的{{page_id}} 在用户与网页交互时发生变化(使用下拉选择器)。以下 javascript 将能够捕捉到这一点:
<script>
function post_redirect_link() {
var page_id = document.getElementById("sel1").options.selectedIndex;
return '/res/{{link}}/'+page_id+'/{{email}}/'
}
</script>
我尝试将action动态设置为:
<form method='POST' action='post_redirect_link()'>
<input type="hidden" name="email" value="{{email}}">
<input type="image" type='submit' src={% static "assets/images/twitter_icon.png" %}>
</form>
但是,我得到了/res/correct_link/post_redirect_link()/correct_email 类型的重定向,其中“post_redirect_link()”被视为字符串,而不是 js 方法返回的值。
有什么建议吗?
【问题讨论】:
-
尝试执行
<form action="javascript:;" onsubmit="myFunction(this)">并在您的函数中,对您想要的任何地址进行post调用。
标签: javascript jquery html django post