0

我在 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 ......

请帮忙

4

1 回答 1

0

在您的 JS/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(currentIp){
                $.ajax({
                  type: "POST",
                  url: "http://192.168.1.1:8081/clicked",
                  data: {'current_ip' :currentIp},
                  success: success,
                  dataType: dataType
               });
            }
        </script>

    <form>
        <ul id="Devicesid">
        {% for Devices in ip %}
            <li class="label even-row">
                 <a value="btnSend"><button onclick="goPython(Devices.ip)" 
 type="button" name="btnSend">{{ Devices.ip }}</button></</a>
            </li>
        {% endfor %}

        </ul>
    </form>
    </body>
    {% endblock %}

    </html>

And in your Flask code,  do this :

    @main.route('/clicked',methods = ['POST', 'GET'])
    def clicked():
            IP = request.json.get('current_ip', '')
            URL = 'https://' + IP

            credentials = {'username': 'apiuser', 'secretkey': 'f00D$'}
            session = requests.session()
            ###and so on......
于 2020-04-07T14:33:14.430 回答