繁体   English   中英

单击html按钮时如何调用python函数并传递参数?

[英]how to call python function and pass arguments when the html button is clicked?

我显示了来自数据库的数据

{% extends "layout.html" %}
{% block content %}
    <article class="media content-section">
            <div class="table-responsive">          
                <div class="table table-striped">
                    <table>
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Phone Number</th>
                                <th> Send SMS </th>
                            </tr>
                        </thead>
                        {% for detail in details%}
                        <tbody>
                            <tr>
                                <th>{{ detail.username }}</th>
                                <th>{{ detail.phonenumber }}</th>
                                <th><button type="button" class="btn btn-success" onclick="'{{ url_for('sendsms', phonenumber = detail.phonenumber)}}'">Send Request</button></th>
                        </tr>
                    </tbody>
                {% endfor %}
            </table>
        </div>
    </article>
{% endblock content %}

单击按钮时如何调用功能并通过电话号码?

当我按下该按钮时,sendms函数应被调用,电话号码应传递给该函数。

我的路线是

def sendsms(phonenumber):
        account_sid = '************************'
        auth_token = '*************************'
        client = Client(account_sid, auth_token)

        message = client.messages.create(
                                  from_= current_user.username,
                                  body='i need immediately'
                                  to= phonenumber)

        print(message.sid)

好的,咱们走吧。 首先,让我们向HTML代码中添加表单元素。

{% for detail in details%}

#let's make a bunch of forms for every detail so you can send separate data for every request.

    <form action="{{ url_for('sendsms') }}" method="post">
        <tbody>
           <tr>
          <th><input type="text" name="username" value={{ detail.username }} required></th>
          <th><input type="text" name="phonenumber" value={{ detail.phonenumber }} required></th>
          <th><input type="submit" name="button" class="btn btn-success" value="Send Request"></a></th>
          </tr>
        </tbody>
    </form>
        {% endfor %}

然后,让我们处理后端的POST请求。

#Let's make sure that our route handles POST requests so add POST method to route:
@app.route('/sendsms', methods=['POST'])
def sendsms():
    account_sid = '************************'
    auth_token = '*************************'
    client = Client(account_sid, auth_token)

    #You can access values you sent in form using request.form data:
    phonenumber = request.form['phonenumber']
    username = request.form['username']

    message = client.messages.create(
                              from_= username,
                              body='i need immediately'
                              to= phonenumber)

    print(message.sid)

PS此代码可能应该工作,但由于我无法对其进行检查-至少会给您一个提示。 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM