[英]How to display flashing message without reloading the page in Flask?
[英]Message flashing without reloading page?
因此,我有这个非常基本的代码,它需要一些输入文本,并在单击提交按钮后立即显示一条消息。 我的问题是,单击该按钮后,输入的任何文本都会消失,因为发布过程会重新加载html模板。 有办法避免这种情况吗? 谢谢,下面是我的代码:
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()
(我知道)动态更改DOM的唯一方法是通过AJAX。
http://flask.pocoo.org/docs/patterns/jquery/
有一个很好的例子。
基本上,创建您自己的api。
拥有你的索引
@app.route('/')
def index():
return render_template('index.html')
调用视图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)
在您的HTML中,类似于(这不是真实的代码)
<script>
var $whateves= $("#whateves").whateves({
$.getJSON($SCRIPT_ROOT + '/test', {
do something?
}, function(data) {
$("#result").text(data.result);
});
显示新结果
<span id=result>No message to flash</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.