【问题标题】:How to pass the value of a button to Flask Python function如何将按钮的值传递给 Flask Python 函数
【发布时间】:2018-07-06 17:36:01
【问题描述】:

我可以使用request 对象来执行此操作吗?此按钮不是表单按钮。

我的 html 文件名为 display.html,如下所示:

<table style="width:90%">

        <tr>
            <th> pluginid </th>
            <th>name </th>
            <th> exploitavailable</th>
            <th>severity </th>

        </tr>


    {% for element in elements %}
    <tr>
        <td> <button onclick="location.href='display.html';" type="button" value="{{element.split(" ",1)[0]}}" name="{{element.split(" ",1)[0]}}"> {{ element.split(" ",1)[0]}} </button></td>
        <td> {{element.split("\t",1)[0]}} </td>
        <td> {{element.split("\t",2)[1]}} </td>
        <td> {{element.split("\t",3)[2]}} </td>

    </tr>

    {% endfor %}

    </table>

我的 python 代码如下所示:

@app.route("/display.html",methods=['GET', 'POST'])
def display():
    return render_template("display.html")


if __name__ == '__main__':
    app.run(port=8898)

【问题讨论】:

    标签: javascript html python-3.x flask jinja2


    【解决方案1】:

    假设{{element.split(" ",1)[0]}} 是您插件的ID。

    你可以做的是为插件创建一个路由:

    @app.route('/plugin_details/<int:plugin_id>')
    def plugin_details(plugin_id):
        details = get_some_details(plugin_id)
        return render_template('plugin_details.html', details=details)
    

    然后在按钮中使用以下链接链接到此页面:

    &lt;button onclick="window.location='{{url_for('plugin_details', plugin_id=element.split(" ",1)[0])}}';"&gt;Visit Page Now&lt;/button&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多