简体   繁体   English

消息闪烁而不重新加载页面?

[英]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. 我的问题是,单击该按钮后,输入的任何文本都会消失,因为发布过程会重新加载html模板。 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() 

The only way (i'm aware) to dynamically change the DOM is through AJAX. (我知道)动态更改DOM的唯一方法是通过AJAX。

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

has a great example. 有一个很好的例子。

Basically, create your own api. 基本上,创建您自己的api。

have your index veiw 拥有你的索引

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

call a view api 调用视图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) 在您的HTML中,类似于(这不是真实的代码)

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM