user3749611
user3749611

Reputation: 11

Message flashing without reloading page?

so I have this very basic code which takes some input text and flash a message once a submit button has been clicked. My problem is that whatever text was enter disappears after the button is clicked because the post procedure reloads the html template. Is there a way to avoid that ? Thanks , below is my code :

class View(flask.views.MethodView):
    def get(self):
        return flask.render_template('index.html')

    def post(self):
        flask.flash(flask.request.form['expression']) #reprint the text input
        flask.flash(Markup('<br /><h2> Score = '+str(5)+' %</h2>'))
        return self.get() 

Upvotes: 1

Views: 2343

Answers (1)

Chet Meinzer
Chet Meinzer

Reputation: 1741

The only way (i'm aware) to dynamically change the DOM is through AJAX.

http://flask.pocoo.org/docs/patterns/jquery/

has a great example.

Basically, create your own api.

have your index veiw

 @app.route('/')
 def index():
      return render_template('index.html')

call a view api

@app.route('/test')
def post(object):
    getIt= request.args.listvalues()[0]
    More code...
    result=Markup('<br /><h2> Score = '+str(5)+' %</h2>')
    return jsonify(result)

in your HTML, something like (this is not real code)

<script>
var $whateves= $("#whateves").whateves({
$.getJSON($SCRIPT_ROOT + '/test', {
 do something?
  }, function(data) {
    $("#result").text(data.result);
  });

to show the new result

 <span id=result>No message to flash</span>

Upvotes: 3

Related Questions