Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Use javascript to set action in django form

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!