Function return

Asked

Viewed 93 times

0

Good afternoon,

I have these two cases:

  1. I have a modal with <form action="{{url_for('rota')}}" and on this route I am giving an update on the information, my problem is in the return of this POST, which I put to return to modal, if possible with a message "Data updated successfully"

    dados.con_contato = request.form.get('modalTelContato')
    dados.con_contato_observacao = request.form.get('modalRecado')
    
    update = con_contatos.update(dados)
    
    return o que eu coloco aqui???
    
  2. I have a "Save" button set with "Submit" and in my form I have a onsubmit="return closeSelf(this);"

In this script I did so:

function closeSelf (f) {
  f.submit();
  window.close();
}

My problem is that if I leave it like this, it closes without giving Ubmit, if I take out the window.close(), it runs Ubmit but does not close the current window!

  • You have two different questions. You should put them separately.

  • Another tip: Think carefully about the title. It should describe the problem succinctly. Your problem is not knowing how to return a function, but how to respond to an HTTP request.

  • Return should not be the page to which the comrade will be redirected?

  • @Eltonnunes, yes, should show a message of "data changed successfully" and close the modal.

  • I refer to url_for(page,)

1 answer

0


You can use the Flash, only import by flask itself

from flask import Flask, flash

in your route code you return this function to which the html will be ready to receive the message

from flask import Flask, redirect, url_for

dados.con_contato = request.form.get('modalTelContato')
dados.con_contato_observacao = request.form.get('modalRecado')

update = con_contatos.update(dados)

flash('Dados atualizados com sucesso')
return redirect(url_for('*ROTA*'))

in the html page you will use the jinja2(obvious) Example of how you will get this dps message to have rendered the back-end

{% with messages = get_flashed_messages() %}
  {% if messages %}
    {% for message in messages %}
      {{ message }} # aqui você vai receber a mensagem que configurou lá na rota com o 'flash'
    {% endfor %}
  {% endif %}
{% endwith %}

and you’re ready!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.