您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Flask-在按钮OnClick事件上调用python函数

Flask-在按钮OnClick事件上调用python函数

你可以简单地在AJAX的帮助下完成此操作…这是一个示例,该示例调用python函数,该函数在不重定向或刷新页面的情况下打印问候。

在app.py中,将其放在代码段下方。

#rendering the HTML page which has the button
@app.route('/json')
def json():
    return render_template('json.html')

#background process happening without any refreshing
@app.route('/background_process_test')
def background_process_test():
    print ("Hello")
    return ("nothing")

并且你的json.html页面应如下所示。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
        $(function() {
          $('a#test').bind('click', function() {
            $.getJSON('/background_process_test',
                function(data) {
              //do nothing
            });
            return false;
          });
        });
</script>


//button
<div class='container'>
    <h3>Test</h3>
        <form>
            <a href=# id=test><button class='btn btn-default'>Test</button></a>
        </form>

</div>

在这里,当你按下控制台中的“测试简单”按钮时,你可以看到“ Hello”正在显示,而没有任何刷新。

Python 2022/1/1 18:28:49 有571人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶