In django 1.10, I need to pass parameters from one html template back to view.py, and finally render again the same view.
The problem is in setting action in the html to use javascript.
This is my html template:
<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>
{{page_id}} in action changes as the user interacts with the web page (using a drop-down selector). The following javascript would be able to catch this:
<script>
function post_redirect_link() {
var page_id = document.getElementById("sel1").options.selectedIndex;
return '/res/{{link}}/'+page_id+'/{{email}}/'
}
</script>
And I tried to set action dynamically as:
<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>
BUT instead, I get a redirect of the type /res/correct_link/post_redirect_link()/correct_email, where "post_redirect_link()" is taken as string rather than being the value returned by the js method.
Any suggestion?
Try doing the following.
<form action="#" method='POST'>
<script>
function changeAction() {
var page_id = document.getElementById("sel1").options.selectedIndex;
var url = '/res/{{link}}/'+page_id+'/{{email}}/';
document.forms[0].action = url;
}
</script>
<input type="hidden" name="email" value="{{email}}">
<input type="image" type='submit' onmouseover="changeAction()" src={% static "assets/images/twitter_icon.png" %}>
</form>