【发布时间】:2021-10-17 22:33:39
【问题描述】:
我已经编写了一个示例代码,我想通过在烧瓶中单击一个 html 按钮来调用在 python 文件(不是页面,只是函数)中定义的 python 函数。请帮助我找到正确的方法。
下面是我在 base.html 中编写的用于触发按钮单击操作的 jquery 脚本。 button_click() 是 routes.py 中定义的函数,我想在单击按钮时调用它。
<script>
$('#btn-click').on('click', function(e) {
{{button_click()}}
});
</script>
下面是整个代码示例
main.py
from Website import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
init.py(下划线不可见)
from flask import Flask
from .routes import routes
def create_app():
app = Flask(__name__)
#..........................Register blueprint.......................#
app.register_blueprint(routes, url_prefix='/')
return app
base.html
<!DOCTYPE html>
<html lang="english">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href={{url_for('static', filename='images/logo1.png')}}>
<title>{% block title%}{% endblock %}</title>
<!-- Bootstrap CSS CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<!-- Bootstrap icons css -->
<link rel="stylesheet" href={{url_for('static', filename='bootstrap-icons/bootstrap-icons.css')}}>
<!-- Font Awesome JS (icons) -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/solid.js" integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous"></script>
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js" integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous"></script>
<!-- Our Custom CSS -->
<link rel="stylesheet" href={{url_for('static', filename='stylesheets/style.css')}}>
</head>
<body>
<!--............................Home page navbar................................-->
<div class="container-fluid">
<nav class=" navbar navbar-expand-lg navbar-light fixed-top bg-light py-lg-0 " id="customNavbar">
<a class="navbar-brand" href="/">
<!-- Add logo -->
<img src={{url_for('static', filename='images/logo1')}} alt="logo">
</a>
<button class="navbar-toggle-collapsed d-block d-sm-block d-md-none" type="button"
data-toggle="collapse"
data-target="#navbarResponsive"
aria-controls="navbarResponsive"
aria-expanded="true"
aria-label="Toggle navigation"
id="togglerButton">
<span class="line"></span>
<span class="line"></span>
<span class="line" style="margin-bottom: 0;"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="/"><i class="fas fa-home"></i> Home</a>
</li>
</ul>
</div>
<form class="lg-flex">
<!-- Button to call function -->
<button type="button" name='signin-btn' class="btn buttonCustom" id='btn-click'>Click Me</button>
</form>
</nav>
</div>
<!--...............Home content.......................-->
{% block navPages%}{% endblock%}
<!-- javascript to enable bootstrap usage -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8" crossorigin="anonymous"></script>
<script>
$('#btn-click').on('click', function(e) {
{{button_click()}}
});
</script>
</body>
</html>
home.html
{% extends 'base.html'%}
{% block title %}Home{% endblock %}
{% block navPages%}
<h1> Home Page! </h1>
{% endblock%}
【问题讨论】:
-
你可以为你想调用的
api-func这样的函数定义路径,然后使用js中的fetch或者你可以在jquery中使用xhr来调用api-func的url路径 -
你能给我看一个代码sn-p我怎么能做到这一点