Python Flask 使用按钮调用函数 [英] Python Flask calling functions using buttons

查看:127
本文介绍了Python Flask 使用按钮调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下我的烧瓶模板中的按钮后,我希望它调用在 app.py 中定义的 python 函数,我可以通过在下面输入以下定义函数的位置在模板中调用该函数:

Once the button in my flask template is pressed I'd like it to call a python function defined in app.py that I made to be available to be called within the template by typing the following below where I define the function:

app.py 中的示例函数:

Example function in app.py:

@app.route('/foo')
def foo(x,y):
    pass
app.jinja_env.globals.update(foo=foo)

模板:

<button  type="button" onclick="myFunction(this)" name="enable" id="{{counter}}"> Enable </button>

在我的按钮中,我有 onclick 属性只是为了测试使用 javascript 按下了正确的按钮,如下所示:

In my button I have the onclick attribute just to test that the correct button out of many is pressed using javascript like such:

{% block scripts %}
    {{ super() }}
    <script>
    function myFunction(elem){
        if(confirm('Are you sure you want to ' + elem.name) == true){
            alert("its done.");            
         }
         else { 
            return false;
         }          
    }
    </script>
{% endblock %}

我面临的问题是我需要在模板中提供的功能与正确的按钮相对应.例如,如果按钮显示启用,那么我需要调用已经定义的启用功能,否则如果按钮对应于假,我希望使用禁用功能.

The issue I'm facing is I need the function that I'm making available within the template to correspond to the correct button. For example, if the button says Enable, then I need to call the enable function defined already or otherwise if the button corresponds to false, I'd like the disable function to be used.

我觉得我正朝着正确的方向前进,但无法通过这部分.请尽可能详细.

I feel like I'm headed in the right direction but can't get past this part. Please be as detailed as you can.

推荐答案

如果你想在不向服务器生成请求的情况下执行你的函数,那么你的函数必须在 JavaScript 中定义.否则,您需要触发 HTTP 请求.

If you want to execute your function without generating a request to the server, then your function must be defined in JavaScript. Otherwise, you need to fire an HTTP request.

现在就您而言,如果您要做的只是启用/禁用按钮,那么在 javascript 中执行所有操作是有意义的(无需转到服务器).

Now in your case, if all you're trying to do is enable/disable buttons, it would make sense to do all that in javascript (no need to go to the server).

示例:

<button type="button" onclick="disableButton(this)" name="enable">Enable</button>

javascript

function disableButtonState(elem) {
    if(confirm('Are you sure you want to disable this button?') == true) {
        elem.disabled = true;
        alert("its done.");            
    }
    else { 
        return false;
    }          
}
</script>

但是,如果您想要调用服务器上的方法,例如发送电子邮件,那么您应该使用 form POST/GET 或 AJAX发布/获取

However if what you want is to call a method on your server that, for example, sends an email, then you should use a form POST/GET or AJAX POST/GET

示例:

app.py

@app.route('/foo', methods=['GET', 'POST'])
def foo(x=None, y=None):
    # do something to send email
    pass

模板

<form action="/foo" method="post">
    <button type="submit" value="Send Email" />
</form>

当您单击发送电子邮件"按钮时,一个 HTTP POST 请求将发送到您应用程序上的/foo".您的函数 foo 现在可以从请求中提取一些数据,并在服务器端执行它想做的任何事情,然后将响应返回给客户端 Web 浏览器.

When you click the "Send Email" button an HTTP POST request is sent to "/foo" on your application. Your function foo can now extract some data from the request and do whatever it wants to do on the server side and then return a response to the client web browser.

建议阅读 Flask 教程 以获得更好的理解涉及使用 Flask 构建的 Web 应用程序时的客户端/服务器交互.

It would suggest going through the Flask Tutorial to get a better understanding of client/server interactions when it comes to web applications built with Flask.

这篇关于Python Flask 使用按钮调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆