Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda