簡體   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