我在 python 烧瓶环境中需要帮助我有一些 html 页面,在该页面中我从 SQL 数据库获取 IP 地址列表,并且列表中的 IP 地址是可点击的。我需要的是能够点击一些 IP 并能够在 FLASK 的另一个功能中使用该 IP。
部分代码我的代码示例:HTML
<!DOCTYPE html>
<html>
{% extends "base.html" %}
{% block content %}
<body>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
function goPython(){
$.ajax({
url: "/clicked",
context: document.body
}).done(function() {
alert('finished python script');;
});
}
</script>
{% for Devices in ip %}
<form action = "http://192.168.1.1:8081/clicked" method = "post">
<ul id="Devicesid">
<li class="label even-row">
<a onclick="goPython()" value="btnSend"><button type="button" name="btnSend">{{ Devices.ip }}</button></</a>
</li>
</ul>
</form>
</body>
{% endfor %}
</html>
{% endblock %}
和 python main.py 代码的一部分:
@main.route('/clicked',methods = ['POST', 'GET'])
def clicked():
while True:
IP = request.form['btnSend']
URL = 'https://' + IP
credentials = {'username': 'apiuser', 'secretkey': 'f00D$'}
session = requests.session()
###and so on......
正如您在 HTML iam 使用 FOR 循环并从我的数据库中获取 ip 地址的部分中所看到的那样,现在 iam 试图能够单击 IP 地址并将其用于 python FLASK 的另一个功能以连接到该实用程序设备。
我怎样才能做到简单而正确?正如我理解的那样,为了使其工作,我需要使用 AJAX 或 JQuery ......
请帮忙