html中的可单击参数使用flask转到python函数

2024-10-01 04:52:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要python flask环境中的帮助我有一些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地址, 现在,我正试图拥有点击IP地址的能力,并在python FLASK的另一个功能中使用它来连接到实用设备

我怎样才能做到简单和正确? 正如我所理解的,为了让它工作,我需要使用AJAX或JQuery

请帮忙


Tags: 代码ipform数据库flask列表地址html
1条回答
网友
1楼 · 发布于 2024-10-01 04:52:22

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

相关问题 更多 >