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

139
Visualizações
How to send variable data form html to flask

I want to send variable data form html to flask
This is my html js code.
I can not send that variable data to the flask in that way.

<script>
        var data = "shan";
        window.location.href='{{ url_for( "move_forward" , title=data) }}';
</script>

But when I used below way I can send it

<script>
        window.location.href='{{ url_for( "move_forward" , title="shan") }}';
</script>

But finally I want to assign some value to the variable and I want to send it to the flask.
How can I do it

 @app.route("/move_forward/<title>", methods=['GET', 'POST'])
        def move_forward(title):
             print(title)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Jinja templates (the parts using {{ ... }}) are not JavaScript.

When you call render_template, '{{ url_for( "move_forward" , title="shan") }}' gets replaced with something like '/move_forward/shan' and this is what the client receives.

When you do '{{ url_for( "move_forward" , title=data) }}' Flask/Jinja doesn't know what data is because it doesn't interact with the JavaScript, it's just trying to replace a string.

You could instead use a mixture of JavaScript and Jinja to get it working:

<script>
    var baseUrl = '{{ url_for("move_forward") }}';
    var data = "shan";
    window.location.href = baseUrl + "/" + data;
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

Thank you @Henry for your answer and I go trough it and do little modification to do it work.

In HTML js script I modify like bellow

var baseUrl = '{{ url_for("move_forward") }}';
var data = "shan";
window.location.href = baseUrl + "?title=" + data;

and Then I modify the Flask code like bellow.

@app.route("/move_forward", methods=['GET', 'POST'])
        def move_forward():
            if request.method == 'GET':
                title = request.args.get('title')

Now it's work perfectly. Thank you

about 4 years ago · Juan Pablo Isaza 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